|
8 | 8 | "fmt" |
9 | 9 | "log" |
10 | 10 | "os" |
| 11 | + "strconv" |
11 | 12 |
|
12 | 13 | "runtime" |
13 | 14 | "sync" |
@@ -40,6 +41,9 @@ import ( |
40 | 41 | "github.com/wavetermdev/waveterm/pkg/wshutil" |
41 | 42 | "github.com/wavetermdev/waveterm/pkg/wslconn" |
42 | 43 | "github.com/wavetermdev/waveterm/pkg/wstore" |
| 44 | + |
| 45 | + "net/http" |
| 46 | + _ "net/http/pprof" |
43 | 47 | ) |
44 | 48 |
|
45 | 49 | // these are set at build time |
@@ -315,13 +319,42 @@ func clearTempFiles() error { |
315 | 319 | return nil |
316 | 320 | } |
317 | 321 |
|
| 322 | +func maybeStartPprofServer() error { |
| 323 | + pprofPortStr := os.Getenv("WAVETERM_PPROFPORT") |
| 324 | + if pprofPortStr == "" { |
| 325 | + return nil |
| 326 | + } |
| 327 | + defer os.Unsetenv("WAVETERM_PPROFPORT") |
| 328 | + pprofPort, err := strconv.Atoi(pprofPortStr) |
| 329 | + if err != nil { |
| 330 | + return fmt.Errorf("invalid WAVETERM_PPROFPORT value '%s': %v", pprofPortStr, err) |
| 331 | + } |
| 332 | + if pprofPort < 1 || pprofPort > 65535 { |
| 333 | + return fmt.Errorf("WAVETERM_PPROFPORT must be between 1 and 65535, got %d", pprofPort) |
| 334 | + } |
| 335 | + go func() { |
| 336 | + addr := fmt.Sprintf("localhost:%d", pprofPort) |
| 337 | + log.Printf("starting pprof server on %s\n", addr) |
| 338 | + if err := http.ListenAndServe(addr, nil); err != nil { |
| 339 | + log.Printf("[error] pprof server failed: %v\n", err) |
| 340 | + } |
| 341 | + }() |
| 342 | + return nil |
| 343 | +} |
| 344 | + |
318 | 345 | func main() { |
| 346 | + err := maybeStartPprofServer() |
| 347 | + if err != nil { |
| 348 | + log.Printf("[error] %v\n", err) |
| 349 | + return |
| 350 | + } |
| 351 | + |
319 | 352 | log.SetFlags(log.LstdFlags | log.Lmicroseconds) |
320 | 353 | log.SetPrefix("[wavesrv] ") |
321 | 354 | wavebase.WaveVersion = WaveVersion |
322 | 355 | wavebase.BuildTime = BuildTime |
323 | 356 |
|
324 | | - err := grabAndRemoveEnvVars() |
| 357 | + err = grabAndRemoveEnvVars() |
325 | 358 | if err != nil { |
326 | 359 | log.Printf("[error] %v\n", err) |
327 | 360 | return |
|
0 commit comments