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