fix disabled pprof server handling

This commit is contained in:
leonincs
2026-04-23 19:00:13 +08:00
parent d7a0e2a6b7
commit 458f4f361d
3 changed files with 20 additions and 16 deletions

View File

@@ -78,7 +78,9 @@ func main() {
if err != nil { if err != nil {
log.Printf("Failed to start API pprof server: %v", err) 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) r := apphttp.SetRouter(sqlDB, cache, rmq)

View File

@@ -132,7 +132,9 @@ func main() {
if err != nil { if err != nil {
log.Printf("Failed to start worker pprof server: %v", err) log.Printf("Failed to start worker pprof server: %v", err)
} }
defer pprofServer.Close() if pprofServer != nil {
defer pprofServer.Close()
}
errCh := make(chan error, 4) errCh := make(chan error, 4)
log.Printf("Worker started, consuming queue=%s", socialQueue) log.Printf("Worker started, consuming queue=%s", socialQueue)

View File

@@ -9,14 +9,14 @@ import (
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"time" "time"
) )
type PprofServer struct { type PprofServer struct {
name string name string
server *http.Server server *http.Server
shutdownTimeout time.Duration shutdownTimeout time.Duration
} }
func NewPprofMux() *http.ServeMux { func NewPprofMux() *http.ServeMux {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/", pprof.Index)
@@ -29,20 +29,20 @@ func NewPprofMux() *http.ServeMux {
} }
func NewPprofServer(name string, enabled bool, addr string) (*PprofServer, error) { func NewPprofServer(name string, enabled bool, addr string) (*PprofServer, error) {
pprofServer := &PprofServer{
name: name,
shutdownTimeout: 3 * time.Second,
}
if !enabled || addr == "" { if !enabled || addr == "" {
return pprofServer, nil return nil, nil
} }
ln, err := net.Listen("tcp", addr) ln, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to start %s pprof server on %s: %w", name, addr, err) 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{ pprofServer.server = &http.Server{
Addr: addr, Addr: addr,
Handler: NewPprofMux(), Handler: NewPprofMux(),
ReadHeaderTimeout: 5 * time.Second, ReadHeaderTimeout: 5 * time.Second,
} }
go func() { go func() {
@@ -54,7 +54,7 @@ func NewPprofServer(name string, enabled bool, addr string) (*PprofServer, error
return pprofServer, nil return pprofServer, nil
} }
func Shutdown(ctx context.Context, srv *http.Server) error{ func Shutdown(ctx context.Context, srv *http.Server) error {
if srv == nil { if srv == nil {
return nil return nil
} }
@@ -65,11 +65,11 @@ func (s *PprofServer) Close() error {
if s == nil { if s == nil {
return nil return nil
} }
shutdownCtx, cancel := context.WithTimeout(context.Background(), 3 * time.Second) shutdownCtx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer cancel() defer cancel()
if err := Shutdown(shutdownCtx, s.server); err != nil { if err := Shutdown(shutdownCtx, s.server); err != nil {
log.Printf("Failed to shutdown %s pprof server: %v", s.name, err) log.Printf("Failed to shutdown %s pprof server: %v", s.name, err)
return err return err
} }
return nil return nil
} }