Merge pull request #7777 from yyforyongyu/fix-pprof-redirect

lnd: fix pprof server redirects
This commit is contained in:
Oliver Gugger 2023-06-22 09:20:31 +02:00 committed by GitHub
commit 13e198fbde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

32
lnd.go
View File

@ -11,9 +11,9 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
_ "net/http/pprof" // nolint:gosec // used to set up profiling HTTP handlers. "net/http/pprof"
"os" "os"
"runtime/pprof" runtimePprof "runtime/pprof"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -187,16 +187,26 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
// Enable http profiling server if requested. // Enable http profiling server if requested.
if cfg.Profile != "" { if cfg.Profile != "" {
// Create the http handler. // Create the http handler.
profileRedirect := http.RedirectHandler( pprofMux := http.NewServeMux()
"/debug/pprof", http.StatusSeeOther, pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
) pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
http.Handle("/", profileRedirect) pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
// Redirect all requests to the pprof handler, thus visiting
// `127.0.0.1:6060` will be redirected to
// `127.0.0.1:6060/debug/pprof`.
pprofMux.Handle("/", http.RedirectHandler(
"/debug/pprof/", http.StatusSeeOther,
))
ltndLog.Infof("Pprof listening on %v", cfg.Profile) ltndLog.Infof("Pprof listening on %v", cfg.Profile)
// Create the pprof server. // Create the pprof server.
pprofServer := &http.Server{ pprofServer := &http.Server{
Addr: cfg.Profile, Addr: cfg.Profile,
Handler: profileRedirect, Handler: pprofMux,
ReadHeaderTimeout: 5 * time.Second, ReadHeaderTimeout: 5 * time.Second,
} }
@ -225,9 +235,11 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
if err != nil { if err != nil {
return mkErr("unable to create CPU profile: %v", err) return mkErr("unable to create CPU profile: %v", err)
} }
pprof.StartCPUProfile(f) _ = runtimePprof.StartCPUProfile(f)
defer f.Close() defer func() {
defer pprof.StopCPUProfile() _ = f.Close()
}()
defer runtimePprof.StopCPUProfile()
} }
// Run configuration dependent DB pre-initialization. Note that this // Run configuration dependent DB pre-initialization. Note that this