fix disabled pprof server handling
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user