Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): pprof in http server #1515

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,14 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
// The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.
// TODO: write test for me.
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool), enableDebugger bool) (
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error,
) {
conf, gen, err := MakeConfig(workingDir)
if err != nil {
return nil, nil, err
}

conf.HTTP.EnableDebugger = enableDebugger

defaultWalletPath := PactusDefaultWalletPath(workingDir)
walletInstance, err := wallet.Open(defaultWalletPath, true,
wallet.WithCustomServers([]string{conf.GRPC.Listen}))
Expand Down
4 changes: 1 addition & 3 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func buildStartCmd(parentCmd *cobra.Command) {
passwordOpt := startCmd.Flags().StringP("password", "p", "",
"the wallet password")

debuggerOpt := startCmd.Flags().BoolP("debug", "d", false, "enable pprof debugger")

startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
// change working directory
Expand Down Expand Up @@ -60,7 +58,7 @@ func buildStartCmd(parentCmd *cobra.Command) {
return password, true
}
node, _, err := cmd.StartNode(
workingDir, passwordFetcher, *debuggerOpt)
workingDir, passwordFetcher)
cmd.FatalErrorCheck(err)

cmd.TrapSignal(func() {
Expand Down
4 changes: 1 addition & 3 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const appID = "com.github.pactus-project.pactus.pactus-gui"

var (
workingDirOpt *string
debuggerOpt *bool
passwordOpt *string
testnetOpt *bool
)
Expand All @@ -34,7 +33,6 @@ func init() {
workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path")
passwordOpt = flag.String("password", "", "wallet password")
testnetOpt = flag.Bool("testnet", false, "initializing for the testnet")
debuggerOpt = flag.Bool("debug", false, "enable pprof debugger")
version.NodeAgent.AppType = "gui"
// the gtk on macos should run on main thread.
if runtime.GOOS == "darwin" {
Expand Down Expand Up @@ -157,7 +155,7 @@ func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {

return getWalletPassword(wlt)
}
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher, *debuggerOpt)
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func DefaultConfigMainnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = false
conf.HTTP.Listen = "127.0.0.1:80"
conf.HTTP.EnablePprof = false
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://127.0.0.1:40899"

Expand Down Expand Up @@ -187,6 +188,7 @@ func DefaultConfigTestnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = false
conf.HTTP.Listen = "[::]:80"
conf.HTTP.EnablePprof = false
conf.Nanomsg.Enable = false
conf.Nanomsg.Listen = "tcp://[::]:40799"

Expand Down Expand Up @@ -214,6 +216,7 @@ func DefaultConfigLocalnet() *Config {
conf.JSONRPC.Listen = "127.0.0.1:8545"
conf.HTTP.Enable = true
conf.HTTP.Listen = "[::]:0"
conf.HTTP.EnablePprof = true
conf.Nanomsg.Enable = true
conf.Nanomsg.Listen = "tcp://[::]:40799"

Expand Down
5 changes: 5 additions & 0 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
# `listen` is the address to listen for incoming connections for HTTP server.
listen = "127.0.0.1:80"

# `enable_pprof` Enables Golang's pprof debugger for profiling CPU, memory, and goroutines,
# providing key performance insights. Be cautious as it exposes sensitive
# data, so enable only in secure environments with restricted access.
enable_pprof = false

# Nanomsg configuration.
[nanomsg]

Expand Down
12 changes: 6 additions & 6 deletions www/http/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package http

type Config struct {
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnableDebugger bool `toml:"-"` // EnableDebugger is private configs
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnablePprof bool `toml:"enable_pprof"`
}

func DefaultConfig() *Config {
return &Config{
Enable: false,
Listen: "",
EnableDebugger: false,
Enable: false,
Listen: "",
EnablePprof: false,
}
}

Expand Down
13 changes: 11 additions & 2 deletions www/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type Server struct {
logger *logger.SubLogger
}

// init disables default pprof handlers registered by importing net/http/pprof.
// Your pprof is showing (https://mmcloughlin.com/posts/your-pprof-is-showing)
func init() {
http.DefaultServeMux = http.NewServeMux()
}

func NewServer(conf *Config, enableAuth bool) *Server {
ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -87,8 +93,11 @@ func (s *Server) StartServer(grpcServer string) error {
s.router.HandleFunc("/validator/number/{number}", s.GetValidatorByNumberHandler)
s.router.HandleFunc("/metrics/prometheus", promhttp.Handler().ServeHTTP)

if s.config.EnableDebugger {
s.router.HandleFunc("/debug/pprof/", pprof.Index)
if s.config.EnablePprof {
http.HandleFunc("/debug/pprof/", pprof.Index)
http.HandleFunc("/debug/pprof/profile", pprof.Profile)
http.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
http.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

if s.enableAuth {
Expand Down
Loading