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 {
log.Printf("Failed to start API pprof server: %v", err)
}
if pprofServer != nil {
defer pprofServer.Close()
}
// 设置路由
r := apphttp.SetRouter(sqlDB, cache, rmq)

View File

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

View File

@@ -9,7 +9,6 @@ import (
"net/http"
"net/http/pprof"
"time"
)
type PprofServer struct {
@@ -17,6 +16,7 @@ type PprofServer struct {
server *http.Server
shutdownTimeout time.Duration
}
func NewPprofMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
@@ -29,17 +29,17 @@ 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(),
@@ -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,7 +65,7 @@ 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)