mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-09 19:58:45 -05:00
Use pprof build tag for pprof debugging
This commit is contained in:
parent
a57dfe2aad
commit
0809cdedfe
@ -3,14 +3,9 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
_ "net/http/pprof"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
core "github.com/v2fly/v2ray-core/v5"
|
core "github.com/v2fly/v2ray-core/v5"
|
||||||
"github.com/v2fly/v2ray-core/v5/common/cmdarg"
|
"github.com/v2fly/v2ray-core/v5/common/cmdarg"
|
||||||
@ -18,59 +13,16 @@ import (
|
|||||||
"github.com/v2fly/v2ray-core/v5/main/commands/base"
|
"github.com/v2fly/v2ray-core/v5/main/commands/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdRun runs V2Ray with config
|
|
||||||
var CmdRun = &base.Command{
|
|
||||||
CustomFlags: true,
|
|
||||||
UsageLine: "{{.Exec}} run [-c config.json] [-d dir]",
|
|
||||||
Short: "run V2Ray with config",
|
|
||||||
Long: `
|
|
||||||
Run V2Ray with config.
|
|
||||||
|
|
||||||
{{.Exec}} will also use the config directory specified by environment
|
|
||||||
variable "v2ray.location.confdir". If no config found, it tries
|
|
||||||
to load config from one of below:
|
|
||||||
|
|
||||||
1. The default "config.json" in the current directory
|
|
||||||
2. The config file from ENV "v2ray.location.config"
|
|
||||||
3. The stdin if all failed above
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
|
|
||||||
-c, -config <file>
|
|
||||||
Config file for V2Ray. Multiple assign is accepted.
|
|
||||||
|
|
||||||
-d, -confdir <dir>
|
|
||||||
A directory with config files. Multiple assign is accepted.
|
|
||||||
|
|
||||||
-r
|
|
||||||
Load confdir recursively.
|
|
||||||
|
|
||||||
-format <format>
|
|
||||||
Format of config input. (default "auto")
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
|
|
||||||
{{.Exec}} {{.LongName}} -c config.json
|
|
||||||
{{.Exec}} {{.LongName}} -d path/to/dir
|
|
||||||
|
|
||||||
Use "{{.Exec}} help format-loader" for more information about format.
|
|
||||||
`,
|
|
||||||
Run: executeRun,
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configFiles cmdarg.Arg
|
configFiles cmdarg.Arg
|
||||||
configDirs cmdarg.Arg
|
configDirs cmdarg.Arg
|
||||||
configFormat *string
|
configFormat *string
|
||||||
configDirRecursively *bool
|
configDirRecursively *bool
|
||||||
|
|
||||||
configPprof *string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setConfigFlags(cmd *base.Command) {
|
func setConfigFlags(cmd *base.Command) {
|
||||||
configFormat = cmd.Flag.String("format", core.FormatAuto, "")
|
configFormat = cmd.Flag.String("format", core.FormatAuto, "")
|
||||||
configDirRecursively = cmd.Flag.Bool("r", false, "")
|
configDirRecursively = cmd.Flag.Bool("r", false, "")
|
||||||
configPprof = cmd.Flag.String("pprof", "", "")
|
|
||||||
|
|
||||||
cmd.Flag.Var(&configFiles, "config", "")
|
cmd.Flag.Var(&configFiles, "config", "")
|
||||||
cmd.Flag.Var(&configFiles, "c", "")
|
cmd.Flag.Var(&configFiles, "c", "")
|
||||||
@ -78,39 +30,6 @@ func setConfigFlags(cmd *base.Command) {
|
|||||||
cmd.Flag.Var(&configDirs, "d", "")
|
cmd.Flag.Var(&configDirs, "d", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeRun(cmd *base.Command, args []string) {
|
|
||||||
setConfigFlags(cmd)
|
|
||||||
cmd.Flag.Parse(args)
|
|
||||||
printVersion()
|
|
||||||
configFiles = getConfigFilePath()
|
|
||||||
server, err := startV2Ray()
|
|
||||||
if err != nil {
|
|
||||||
base.Fatalf("Failed to start: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if addr := *configPprof; addr != "" {
|
|
||||||
go func() {
|
|
||||||
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := server.Start(); err != nil {
|
|
||||||
base.Fatalf("Failed to start: %s", err)
|
|
||||||
}
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
// Explicitly triggering GC to remove garbage from config loading.
|
|
||||||
runtime.GC()
|
|
||||||
|
|
||||||
{
|
|
||||||
osSignals := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
|
||||||
<-osSignals
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileExists(file string) bool {
|
func fileExists(file string) bool {
|
||||||
info, err := os.Stat(file)
|
info, err := os.Stat(file)
|
||||||
return err == nil && !info.IsDir()
|
return err == nil && !info.IsDir()
|
||||||
|
78
main/commands/run_default.go
Normal file
78
main/commands/run_default.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
//go:build !pprof
|
||||||
|
// +build !pprof
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/v2fly/v2ray-core/v5/main/commands/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdRun runs V2Ray with config
|
||||||
|
var CmdRun = &base.Command{
|
||||||
|
CustomFlags: true,
|
||||||
|
UsageLine: "{{.Exec}} run [-c config.json] [-d dir]",
|
||||||
|
Short: "run V2Ray with config",
|
||||||
|
Long: `
|
||||||
|
Run V2Ray with config.
|
||||||
|
|
||||||
|
{{.Exec}} will also use the config directory specified by environment
|
||||||
|
variable "v2ray.location.confdir". If no config found, it tries
|
||||||
|
to load config from one of below:
|
||||||
|
|
||||||
|
1. The default "config.json" in the current directory
|
||||||
|
2. The config file from ENV "v2ray.location.config"
|
||||||
|
3. The stdin if all failed above
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
|
||||||
|
-c, -config <file>
|
||||||
|
Config file for V2Ray. Multiple assign is accepted.
|
||||||
|
|
||||||
|
-d, -confdir <dir>
|
||||||
|
A directory with config files. Multiple assign is accepted.
|
||||||
|
|
||||||
|
-r
|
||||||
|
Load confdir recursively.
|
||||||
|
|
||||||
|
-format <format>
|
||||||
|
Format of config input. (default "auto")
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
{{.Exec}} {{.LongName}} -c config.json
|
||||||
|
{{.Exec}} {{.LongName}} -d path/to/dir
|
||||||
|
|
||||||
|
Use "{{.Exec}} help format-loader" for more information about format.
|
||||||
|
`,
|
||||||
|
Run: executeRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeRun(cmd *base.Command, args []string) {
|
||||||
|
setConfigFlags(cmd)
|
||||||
|
cmd.Flag.Parse(args)
|
||||||
|
printVersion()
|
||||||
|
configFiles = getConfigFilePath()
|
||||||
|
server, err := startV2Ray()
|
||||||
|
if err != nil {
|
||||||
|
base.Fatalf("Failed to start: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := server.Start(); err != nil {
|
||||||
|
base.Fatalf("Failed to start: %s", err)
|
||||||
|
}
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
// Explicitly triggering GC to remove garbage from config loading.
|
||||||
|
runtime.GC()
|
||||||
|
|
||||||
|
{
|
||||||
|
osSignals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
||||||
|
<-osSignals
|
||||||
|
}
|
||||||
|
}
|
93
main/commands/run_pprof.go
Normal file
93
main/commands/run_pprof.go
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
//go:build pprof
|
||||||
|
// +build pprof
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/v2fly/v2ray-core/v5/main/commands/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CmdRun runs V2Ray with config
|
||||||
|
var CmdRun = &base.Command{
|
||||||
|
CustomFlags: true,
|
||||||
|
UsageLine: "{{.Exec}} run [-c config.json] [-d dir] [-pprof port]",
|
||||||
|
Short: "run V2Ray with config",
|
||||||
|
Long: `
|
||||||
|
Run V2Ray with config.
|
||||||
|
|
||||||
|
{{.Exec}} will also use the config directory specified by environment
|
||||||
|
variable "v2ray.location.confdir". If no config found, it tries
|
||||||
|
to load config from one of below:
|
||||||
|
|
||||||
|
1. The default "config.json" in the current directory
|
||||||
|
2. The config file from ENV "v2ray.location.config"
|
||||||
|
3. The stdin if all failed above
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
|
||||||
|
-c, -config <file>
|
||||||
|
Config file for V2Ray. Multiple assign is accepted.
|
||||||
|
|
||||||
|
-d, -confdir <dir>
|
||||||
|
A directory with config files. Multiple assign is accepted.
|
||||||
|
|
||||||
|
-r
|
||||||
|
Load confdir recursively.
|
||||||
|
|
||||||
|
-format <format>
|
||||||
|
Format of config input. (default "auto")
|
||||||
|
|
||||||
|
-pprof <port>
|
||||||
|
HTTP server port for pprof debugging. (default "8080")
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
{{.Exec}} {{.LongName}} -c config.json
|
||||||
|
{{.Exec}} {{.LongName}} -d path/to/dir
|
||||||
|
|
||||||
|
Use "{{.Exec}} help format-loader" for more information about format.
|
||||||
|
`,
|
||||||
|
Run: executeRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeRun(cmd *base.Command, args []string) {
|
||||||
|
setConfigFlags(cmd)
|
||||||
|
pprof := cmd.Flag.String("pprof", ":8080", "")
|
||||||
|
cmd.Flag.Parse(args)
|
||||||
|
printVersion()
|
||||||
|
configFiles = getConfigFilePath()
|
||||||
|
server, err := startV2Ray()
|
||||||
|
if err != nil {
|
||||||
|
base.Fatalf("Failed to start: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if addr := *pprof; addr != "" {
|
||||||
|
go func() {
|
||||||
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := server.Start(); err != nil {
|
||||||
|
base.Fatalf("Failed to start: %s", err)
|
||||||
|
}
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
// Explicitly triggering GC to remove garbage from config loading.
|
||||||
|
runtime.GC()
|
||||||
|
|
||||||
|
{
|
||||||
|
osSignals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
||||||
|
<-osSignals
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user