From a933f1513f44f36e025811eaf0a64e15948f92d1 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Mon, 23 May 2022 00:34:16 -0700 Subject: [PATCH] Add pprof endpoint when running in dev mode (#708) --- cmd/server/main.go | 19 +------------------ pkg/service/server.go | 18 ++++++++++-------- pkg/service/wire_gen.go | 4 ++++ test/integration_helpers.go | 2 -- test/webhook_test.go | 1 - 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index c7304026..a7a00c39 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -86,17 +86,13 @@ func main() { EnvVars: []string{"LIVEKIT_TURN_KEY"}, }, // debugging flags - &cli.StringFlag{ - Name: "cpuprofile", - Usage: "write cpu profile to `file`", - }, &cli.StringFlag{ Name: "memprofile", Usage: "write memory profile to `file`", }, &cli.BoolFlag{ Name: "dev", - Usage: "sets log-level to debug, and console formatter", + Usage: "sets log-level to debug, console formatter, and /debug/pprof", }, }, Action: startServer, @@ -159,7 +155,6 @@ func getConfig(c *cli.Context) (*config.Config, error) { func startServer(c *cli.Context) error { rand.Seed(time.Now().UnixNano()) - cpuProfile := c.String("cpuprofile") memProfile := c.String("memprofile") conf, err := getConfig(c) @@ -169,18 +164,6 @@ func startServer(c *cli.Context) error { serverlogger.InitFromConfig(conf.Logging) - if cpuProfile != "" { - if f, err := os.Create(cpuProfile); err != nil { - return err - } else { - defer f.Close() - if err := pprof.StartCPUProfile(f); err != nil { - return err - } - defer pprof.StopCPUProfile() - } - } - if memProfile != "" { if f, err := os.Create(memProfile); err != nil { return err diff --git a/pkg/service/server.go b/pkg/service/server.go index 79520b26..aec93abd 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "net/http" + _ "net/http/pprof" "runtime/pprof" "time" @@ -16,13 +17,12 @@ import ( "github.com/urfave/negroni" "go.uber.org/atomic" - "github.com/livekit/protocol/auth" - "github.com/livekit/protocol/livekit" - "github.com/livekit/protocol/logger" - "github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/routing" "github.com/livekit/livekit-server/version" + "github.com/livekit/protocol/auth" + "github.com/livekit/protocol/livekit" + "github.com/livekit/protocol/logger" ) type LivekitServer struct { @@ -85,16 +85,18 @@ func NewLivekitServer(conf *config.Config, recServer := livekit.NewRecordingServiceServer(recService) mux := http.NewServeMux() + if conf.Development { + // pprof handlers are registered onto DefaultServeMux + mux = http.DefaultServeMux + mux.HandleFunc("/debug/goroutine", s.debugGoroutines) + mux.HandleFunc("/debug/rooms", s.debugInfo) + } mux.Handle(roomServer.PathPrefix(), roomServer) mux.Handle(egressServer.PathPrefix(), egressServer) mux.Handle(recServer.PathPrefix(), recServer) mux.Handle("/rtc", rtcService) mux.HandleFunc("/rtc/validate", rtcService.Validate) mux.HandleFunc("/", s.healthCheck) - if conf.Development { - mux.HandleFunc("/debug/goroutine", s.debugGoroutines) - mux.HandleFunc("/debug/rooms", s.debugInfo) - } s.httpServer = &http.Server{ Addr: fmt.Sprintf(":%d", conf.Port), diff --git a/pkg/service/wire_gen.go b/pkg/service/wire_gen.go index cfe30961..45541d37 100644 --- a/pkg/service/wire_gen.go +++ b/pkg/service/wire_gen.go @@ -24,6 +24,10 @@ import ( "os" ) +import ( + _ "net/http/pprof" +) + // Injectors from wire.go: func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*LivekitServer, error) { diff --git a/test/integration_helpers.go b/test/integration_helpers.go index 2a19edca..8d55a893 100644 --- a/test/integration_helpers.go +++ b/test/integration_helpers.go @@ -140,7 +140,6 @@ func createSingleNodeServer(configUpdater func(*config.Config)) *service.Livekit if err != nil { panic(fmt.Sprintf("could not create config: %v", err)) } - conf.Development = true conf.Keys = map[string]string{testApiKey: testApiSecret} if configUpdater != nil { configUpdater(conf) @@ -171,7 +170,6 @@ func createMultiNodeServer(nodeID string, port uint32) *service.LivekitServer { conf.RTC.UDPPort = port + 1 conf.RTC.TCPPort = port + 2 conf.Redis.Address = "localhost:6379" - conf.Development = true conf.Keys = map[string]string{testApiKey: testApiSecret} currentNode, err := routing.NewLocalNode(conf) diff --git a/test/webhook_test.go b/test/webhook_test.go index fdd40b30..01c4120f 100644 --- a/test/webhook_test.go +++ b/test/webhook_test.go @@ -111,7 +111,6 @@ func setupServerWithWebhook() (server *service.LivekitServer, testServer *webhoo } conf.WebHook.URLs = []string{"http://localhost:7890"} conf.WebHook.APIKey = testApiKey - conf.Development = true conf.Keys = map[string]string{testApiKey: testApiSecret} testServer = newTestServer(":7890")