Skip to content

Commit 9d964cb

Browse files
authored
feat: support enable the pprof (#152)
Signed-off-by: chlins <[email protected]>
1 parent 7e02793 commit 9d964cb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cmd/root.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package cmd
1818

1919
import (
20+
"log"
21+
"net/http"
22+
_ "net/http/pprof"
2023
"os"
2124
"os/signal"
2225
"syscall"
@@ -38,6 +41,18 @@ var rootCmd = &cobra.Command{
3841
DisableAutoGenTag: true,
3942
SilenceUsage: true,
4043
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
44+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
45+
// Start pprof server if enabled.
46+
if rootConfig.Pprof {
47+
go func() {
48+
err := http.ListenAndServe(rootConfig.PprofAddr, nil)
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
}()
53+
}
54+
return nil
55+
},
4156
}
4257

4358
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -66,6 +81,8 @@ func init() {
6681
// Bind more cache specific persistent flags.
6782
flags := rootCmd.PersistentFlags()
6883
flags.StringVar(&rootConfig.StoargeDir, "storage-dir", rootConfig.StoargeDir, "specify the storage directory for modctl")
84+
flags.BoolVar(&rootConfig.Pprof, "pprof", rootConfig.Pprof, "enable pprof")
85+
flags.StringVar(&rootConfig.PprofAddr, "pprof-addr", rootConfig.PprofAddr, "specify the address for pprof")
6986

7087
// Bind common flags.
7188
if err := viper.BindPFlags(flags); err != nil {

pkg/config/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323

2424
type Root struct {
2525
StoargeDir string
26+
Pprof bool
27+
PprofAddr string
2628
}
2729

2830
func NewRoot() (*Root, error) {
@@ -33,5 +35,7 @@ func NewRoot() (*Root, error) {
3335

3436
return &Root{
3537
StoargeDir: filepath.Join(user.HomeDir, ".modctl"),
38+
Pprof: false,
39+
PprofAddr: "localhost:6060",
3640
}, nil
3741
}

0 commit comments

Comments
 (0)