5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-24 02:15:40 +00:00
yggdrasil-go/src/core/debug.go

35 lines
849 B
Go
Raw Normal View History

2021-09-23 09:34:58 +00:00
//go:build debug
// +build debug
2021-05-23 19:42:26 +00:00
package core
2017-12-29 04:16:20 +00:00
import "fmt"
import _ "net/http/pprof"
import "net/http"
import "runtime"
import "os"
2019-01-27 13:33:32 +00:00
import "github.com/gologme/log"
// Start the profiler in debug builds, if the required environment variable is set.
func init() {
envVarName := "PPROFLISTEN"
hostPort := os.Getenv(envVarName)
switch {
case hostPort == "":
fmt.Fprintf(os.Stderr, "DEBUG: %s not set, profiler not started.\n", envVarName)
default:
fmt.Fprintf(os.Stderr, "DEBUG: Starting pprof on %s\n", hostPort)
go func() { fmt.Println(http.ListenAndServe(hostPort, nil)) }()
}
}
// Starts the function profiler. This is only supported when built with
// '-tags build'.
func StartProfiler(log *log.Logger) error {
runtime.SetBlockProfileRate(1)
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
return nil
}