Skip to content

Commit

Permalink
pprof should be initialized only once per process (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
wxing1292 authored Jan 11, 2018
1 parent 7ca011c commit 113a644
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions common/service/config/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package config
import (
"fmt"
"net/http"
"sync/atomic"
// DO NOT REMOVE THE LINE BELOW
_ "net/http/pprof"

Expand All @@ -37,6 +38,15 @@ type (
}
)

const (
pprofNotInitialized int32 = 0
pprofInitialized int32 = 1
)

// the pprof should only be initialized once per process
// otherwise, the caller / worker will experience weird issue
var pprofStatus = pprofNotInitialized

// NewInitializer create a new instance of PProf Initializer
func (cfg *PProf) NewInitializer(logger bark.Logger) *PProfInitializerImpl {
return &PProfInitializerImpl{
Expand All @@ -53,10 +63,11 @@ func (initializer *PProfInitializerImpl) Start() error {
return nil
}

go func() {
initializer.Logger.Info("PProf listen on %d", port)
http.ListenAndServe(fmt.Sprintf("localhost:%d", port), nil)
}()

if atomic.CompareAndSwapInt32(&pprofStatus, pprofNotInitialized, pprofInitialized) {
go func() {
initializer.Logger.Infof("PProf listen on %d", port)
http.ListenAndServe(fmt.Sprintf("localhost:%d", port), nil)
}()
}
return nil
}

0 comments on commit 113a644

Please sign in to comment.