-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from onflow/petera/add-profiler
Add profiler server for pprof
- Loading branch information
Showing
5 changed files
with
173 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"strconv" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type ProfileServer struct { | ||
logger zerolog.Logger | ||
server *http.Server | ||
endpoint string | ||
} | ||
|
||
func NewProfileServer( | ||
logger zerolog.Logger, | ||
host string, | ||
port int, | ||
) *ProfileServer { | ||
endpoint := net.JoinHostPort(host, strconv.Itoa(port)) | ||
return &ProfileServer{ | ||
logger: logger, | ||
server: &http.Server{Addr: endpoint}, | ||
endpoint: endpoint, | ||
} | ||
} | ||
|
||
func (h *ProfileServer) ListenAddr() string { | ||
return h.endpoint | ||
} | ||
|
||
func (s *ProfileServer) Start() { | ||
go func() { | ||
err := s.server.ListenAndServe() | ||
if err != nil { | ||
if errors.Is(err, http.ErrServerClosed) { | ||
s.logger.Warn().Msg("Profiler server shutdown") | ||
return | ||
} | ||
s.logger.Err(err).Msg("failed to start Profiler server") | ||
panic(err) | ||
} | ||
}() | ||
} | ||
|
||
func (s *ProfileServer) Stop() error { | ||
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) | ||
defer cancel() | ||
|
||
return s.server.Shutdown(ctx) | ||
} | ||
|
||
func (s *ProfileServer) Close() error { | ||
return s.server.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters