fix disabled pprof server handling
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
if pprofServer != nil {
|
||||||
defer pprofServer.Close()
|
defer pprofServer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// 设置路由
|
// 设置路由
|
||||||
r := apphttp.SetRouter(sqlDB, cache, rmq)
|
r := apphttp.SetRouter(sqlDB, cache, rmq)
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
if pprofServer != nil {
|
||||||
defer pprofServer.Close()
|
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)
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PprofServer struct {
|
type PprofServer struct {
|
||||||
@@ -17,6 +16,7 @@ type PprofServer struct {
|
|||||||
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,17 +29,17 @@ 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(),
|
||||||
@@ -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,7 +65,7 @@ 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user