Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

60 lines
1.8 KiB
Go

package main
import (
"net"
"net/http"
_ "net/http/pprof" // registers /debug/pprof/* on http.DefaultServeMux
"os"
"sync/atomic"
"go.uber.org/zap"
)
// pprofAddr holds the bound address of the daemon's pprof listener
// (empty when pprof is disabled). Read by controller.Status so the
// active address is reported to clients; written exactly once from
// startPProfIfEnabled. atomic.Value guards against the data race that
// would otherwise exist between Status calls and startup.
var pprofAddr atomic.Value
func init() {
pprofAddr.Store("")
}
// daemonPProfAddr returns the currently-bound pprof listener address,
// or an empty string when no listener is running.
func daemonPProfAddr() string {
v, _ := pprofAddr.Load().(string)
return v
}
// startPProfIfEnabled opens an HTTP pprof listener when the user has
// set GORTEX_DAEMON_PPROF_ADDR (e.g. "127.0.0.1:6060"). Opt-in by
// design — leaving a pprof endpoint on by default would expose the
// daemon's internal state to any process on the machine. The listener
// runs in its own goroutine; failures are logged but don't block
// daemon startup.
func startPProfIfEnabled(logger *zap.Logger) {
addr := os.Getenv("GORTEX_DAEMON_PPROF_ADDR")
if addr == "" {
return
}
ln, err := net.Listen("tcp", addr)
if err != nil {
logger.Warn("daemon: pprof listener failed",
zap.String("addr", addr), zap.Error(err))
return
}
bound := ln.Addr().String()
pprofAddr.Store(bound)
logger.Info("daemon: pprof endpoint open",
zap.String("addr", bound),
zap.String("hint", "go tool pprof -http=: http://"+bound+"/debug/pprof/heap"))
go func() {
// Serve on the DefaultServeMux which net/http/pprof registers on.
if err := http.Serve(ln, nil); err != nil && err != http.ErrServerClosed {
logger.Warn("daemon: pprof serve exited", zap.Error(err))
}
}()
}