mirror of
https://github.com/Xevion/banner.git
synced 2025-12-10 16:06:34 -06:00
Setup pprof profiling server
This commit is contained in:
44
main.go
44
main.go
@@ -9,8 +9,7 @@ import (
|
|||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"strings"
|
||||||
"runtime/pprof"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
_ "time/tzdata"
|
_ "time/tzdata"
|
||||||
@@ -37,8 +36,6 @@ var (
|
|||||||
p *message.Printer = message.NewPrinter(message.MatchLanguage("en"))
|
p *message.Printer = message.NewPrinter(message.MatchLanguage("en"))
|
||||||
CentralTimeLocation *time.Location
|
CentralTimeLocation *time.Location
|
||||||
isClosing bool = false
|
isClosing bool = false
|
||||||
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
|
||||||
memoryProfile = flag.String("memprofile", "", "write memory profile to `file`")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -139,21 +136,21 @@ func initRedis() {
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// CPU Profiling (if requested)
|
|
||||||
if *cpuProfile != "" {
|
|
||||||
f, err := os.Create(*cpuProfile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Stack().Err(err).Msg("could not create CPU profile")
|
|
||||||
}
|
|
||||||
defer f.Close() // error handling omitted for example
|
|
||||||
if err := pprof.StartCPUProfile(f); err != nil {
|
|
||||||
log.Fatal().Stack().Err(err).Msg("could not start CPU profile")
|
|
||||||
}
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
}
|
|
||||||
|
|
||||||
initRedis()
|
initRedis()
|
||||||
|
|
||||||
|
if strings.EqualFold(os.Getenv("PPROF_ENABLE"), "true") {
|
||||||
|
// Start pprof server
|
||||||
|
go func() {
|
||||||
|
port := os.Getenv("PPROF_PORT")
|
||||||
|
log.Info().Str("port", port).Msg("Starting pprof server")
|
||||||
|
err := http.ListenAndServe(":"+port, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Stack().Err(err).Msg("Cannot start pprof server")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Create cookie jar
|
// Create cookie jar
|
||||||
var err error
|
var err error
|
||||||
cookies, err = cookiejar.New(nil)
|
cookies, err = cookiejar.New(nil)
|
||||||
@@ -333,19 +330,6 @@ func main() {
|
|||||||
closingSignal := <-stop
|
closingSignal := <-stop
|
||||||
isClosing = true // TODO: Switch to atomic lock with forced close after 10 seconds
|
isClosing = true // TODO: Switch to atomic lock with forced close after 10 seconds
|
||||||
|
|
||||||
// Write memory profile if requested
|
|
||||||
if *memoryProfile != "" {
|
|
||||||
f, err := os.Create(*memoryProfile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Stack().Err(err).Msg("could not create memory profile")
|
|
||||||
}
|
|
||||||
defer f.Close() // error handling omitted for example
|
|
||||||
runtime.GC() // get up-to-date statistics
|
|
||||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
||||||
log.Fatal().Stack().Err(err).Msg("could not write memory profile")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Defers are called after this
|
// Defers are called after this
|
||||||
log.Warn().Str("signal", closingSignal.String()).Msg("Gracefully shutting down")
|
log.Warn().Str("signal", closingSignal.String()).Msg("Gracefully shutting down")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user