From 458f4f361d0def12bcf28069d41cb6110e676ae0 Mon Sep 17 00:00:00 2001 From: leonincs Date: Thu, 23 Apr 2026 19:00:13 +0800 Subject: [PATCH] fix disabled pprof server handling --- backend/cmd/main.go | 4 +++- backend/cmd/worker/main.go | 4 +++- backend/internal/observability/pprof.go | 28 ++++++++++++------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 3861fd6..6793374 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -78,7 +78,9 @@ func main() { if err != nil { log.Printf("Failed to start API pprof server: %v", err) } - defer pprofServer.Close() + if pprofServer != nil { + defer pprofServer.Close() + } // 设置路由 r := apphttp.SetRouter(sqlDB, cache, rmq) diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 10923fc..f277bc0 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -132,7 +132,9 @@ func main() { if err != nil { log.Printf("Failed to start worker pprof server: %v", err) } - defer pprofServer.Close() + if pprofServer != nil { + defer pprofServer.Close() + } errCh := make(chan error, 4) log.Printf("Worker started, consuming queue=%s", socialQueue) diff --git a/backend/internal/observability/pprof.go b/backend/internal/observability/pprof.go index d4d470f..aeb5237 100644 --- a/backend/internal/observability/pprof.go +++ b/backend/internal/observability/pprof.go @@ -9,14 +9,14 @@ import ( "net/http" "net/http/pprof" "time" - ) type PprofServer struct { - name string - server *http.Server + name string + server *http.Server shutdownTimeout time.Duration } + func NewPprofMux() *http.ServeMux { mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) @@ -29,20 +29,20 @@ func NewPprofMux() *http.ServeMux { } func NewPprofServer(name string, enabled bool, addr string) (*PprofServer, error) { - pprofServer := &PprofServer{ - name: name, - shutdownTimeout: 3 * time.Second, - } if !enabled || addr == "" { - return pprofServer, nil + return nil, nil } ln, err := net.Listen("tcp", addr) if err != nil { return nil, fmt.Errorf("failed to start %s pprof server on %s: %w", name, addr, err) } + pprofServer := &PprofServer{ + name: name, + shutdownTimeout: 3 * time.Second, + } pprofServer.server = &http.Server{ - Addr: addr, - Handler: NewPprofMux(), + Addr: addr, + Handler: NewPprofMux(), ReadHeaderTimeout: 5 * time.Second, } go func() { @@ -54,7 +54,7 @@ func NewPprofServer(name string, enabled bool, addr string) (*PprofServer, error return pprofServer, nil } -func Shutdown(ctx context.Context, srv *http.Server) error{ +func Shutdown(ctx context.Context, srv *http.Server) error { if srv == nil { return nil } @@ -65,11 +65,11 @@ func (s *PprofServer) Close() error { if s == nil { return nil } - shutdownCtx, cancel := context.WithTimeout(context.Background(), 3 * time.Second) + shutdownCtx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout) defer cancel() if err := Shutdown(shutdownCtx, s.server); err != nil { log.Printf("Failed to shutdown %s pprof server: %v", s.name, err) - return err + return err } return nil -} \ No newline at end of file +}