run_pprof.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //go:build pprof
  2. // +build pprof
  3. package commands
  4. import (
  5. "log"
  6. "net/http"
  7. _ "net/http/pprof"
  8. "os"
  9. "os/signal"
  10. "runtime"
  11. "syscall"
  12. "github.com/v2fly/v2ray-core/v5/main/commands/base"
  13. )
  14. // CmdRun runs V2Ray with config
  15. var CmdRun = &base.Command{
  16. CustomFlags: true,
  17. UsageLine: "{{.Exec}} run [-c config.json] [-d dir] [-pprof port]",
  18. Short: "run V2Ray with config",
  19. Long: `
  20. Run V2Ray with config.
  21. {{.Exec}} will also use the config directory specified by environment
  22. variable "v2ray.location.confdir". If no config found, it tries
  23. to load config from one of below:
  24. 1. The default "config.json" in the current directory
  25. 2. The config file from ENV "v2ray.location.config"
  26. 3. The stdin if all failed above
  27. Arguments:
  28. -c, -config <file>
  29. Config file for V2Ray. Multiple assign is accepted.
  30. -d, -confdir <dir>
  31. A directory with config files. Multiple assign is accepted.
  32. -r
  33. Load confdir recursively.
  34. -format <format>
  35. Format of config input. (default "auto")
  36. -pprof <port>
  37. HTTP server port for pprof debugging. (default "8080")
  38. Examples:
  39. {{.Exec}} {{.LongName}} -c config.json
  40. {{.Exec}} {{.LongName}} -d path/to/dir
  41. Use "{{.Exec}} help format-loader" for more information about format.
  42. `,
  43. Run: executeRun,
  44. }
  45. func executeRun(cmd *base.Command, args []string) {
  46. setConfigFlags(cmd)
  47. pprof := cmd.Flag.String("pprof", ":8080", "")
  48. cmd.Flag.Parse(args)
  49. printVersion()
  50. configFiles = getConfigFilePath()
  51. server, err := startV2Ray()
  52. if err != nil {
  53. base.Fatalf("Failed to start: %s", err)
  54. }
  55. if addr := *pprof; addr != "" {
  56. go func() {
  57. if err := http.ListenAndServe(addr, nil); err != nil {
  58. log.Print(err)
  59. }
  60. }()
  61. }
  62. if err := server.Start(); err != nil {
  63. base.Fatalf("Failed to start: %s", err)
  64. }
  65. defer server.Close()
  66. // Explicitly triggering GC to remove garbage from config loading.
  67. runtime.GC()
  68. {
  69. osSignals := make(chan os.Signal, 1)
  70. signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
  71. <-osSignals
  72. }
  73. }