Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pprof flag for debugging #3067

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions main/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/v2fly/v2ray-core/v5/common/cmdarg"
"github.com/v2fly/v2ray-core/v5/common/platform"
"github.com/v2fly/v2ray-core/v5/main/commands/base"
"github.com/v2fly/v2ray-core/v5/main/plugins"
)

// CmdRun runs V2Ray with config
Expand Down Expand Up @@ -75,6 +76,12 @@ func setConfigFlags(cmd *base.Command) {

func executeRun(cmd *base.Command, args []string) {
setConfigFlags(cmd)
var pluginFuncs []func() error
for _, plugin := range plugins.Plugins {
if f := plugin(cmd); f != nil {
pluginFuncs = append(pluginFuncs, f)
}
}
cmd.Flag.Parse(args)
printVersion()
configFiles = getConfigFilePath()
Expand All @@ -83,6 +90,14 @@ func executeRun(cmd *base.Command, args []string) {
base.Fatalf("Failed to start: %s", err)
}

for _, f := range pluginFuncs {
go func(f func() error) {
if err := f(); err != nil {
log.Print(err)
}
}(f)
}

if err := server.Start(); err != nil {
base.Fatalf("Failed to start: %s", err)
}
Expand Down
1 change: 0 additions & 1 deletion main/distro/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
// Developer preview features
_ "github.com/v2fly/v2ray-core/v5/app/instman"
_ "github.com/v2fly/v2ray-core/v5/app/observatory"
_ "github.com/v2fly/v2ray-core/v5/app/restfulapi"
_ "github.com/v2fly/v2ray-core/v5/app/tun"

// Inbound and outbound proxies.
Expand Down
11 changes: 11 additions & 0 deletions main/plugins/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package plugins

import "github.com/v2fly/v2ray-core/v5/main/commands/base"

var Plugins []Plugin

type Plugin func(*base.Command) func() error

func RegisterPlugin(plugin Plugin) {
Plugins = append(Plugins, plugin)
}
29 changes: 29 additions & 0 deletions main/plugins/plugin_pprof/plugin_pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package plugin_pprof

Check failure on line 1 in main/plugins/plugin_pprof/plugin_pprof.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use underscores in package names (stylecheck)

import (
"github.com/v2fly/v2ray-core/v5/main/plugins"

Check failure on line 4 in main/plugins/plugin_pprof/plugin_pprof.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
"net/http"
"net/http/pprof"

Check failure on line 6 in main/plugins/plugin_pprof/plugin_pprof.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 7 in main/plugins/plugin_pprof/plugin_pprof.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/v2fly/v2ray-core (goimports)
"github.com/v2fly/v2ray-core/v5/main/commands/base"
)

var pprofPlugin plugins.Plugin = func(cmd *base.Command) func() error {
addr := cmd.Flag.String("pprof", "", "")
return func() error {
if *addr != "" {
h := http.NewServeMux()
h.HandleFunc("/debug/pprof/", pprof.Index)
h.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
h.HandleFunc("/debug/pprof/profile", pprof.Profile)
h.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
h.HandleFunc("/debug/pprof/trace", pprof.Trace)
return (&http.Server{Addr: *addr, Handler: h}).ListenAndServe()
}
return nil
}
}

func init() {
plugins.RegisterPlugin(pprofPlugin)
}
Loading