Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions cmd/housekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

var (
hkDefaultPprofEnabled = os.Getenv("PPROF") == "1"
hkDefaultPprofListenAddr = common.GetEnv("PPROF_LISTEN_ADDR", "localhost:9064")
Comment thread
ilyaluk marked this conversation as resolved.
hkDefaultListenAddr = common.GetEnv("LISTEN_ADDR", "localhost:9064")
hkDefaultPprofEnabled = os.Getenv("PPROF") == "1"

hkPprofEnabled bool
hkPprofListenAddr string
hkListenAddr string
hkPprofEnabled bool
)

func init() {
Expand All @@ -33,8 +33,15 @@ func init() {

housekeeperCmd.Flags().StringVar(&network, "network", defaultNetwork, "Which network to use")

housekeeperCmd.Flags().BoolVar(&hkPprofEnabled, "pprof", hkDefaultPprofEnabled, "enable pprof API")
housekeeperCmd.Flags().StringVar(&hkPprofListenAddr, "pprof-listen-addr", hkDefaultPprofListenAddr, "listen address for pprof server")
// env for backward compatibility
addrLegacy := common.GetEnv("PPROF_LISTEN_ADDR", "")
if addrLegacy != "" {
logrus.Warn("Using legacy PPROF_LISTEN_ADDR, please update to LISTEN_ADDR")
hkDefaultListenAddr = addrLegacy
}

housekeeperCmd.Flags().StringVar(&hkListenAddr, "listen-addr", hkDefaultListenAddr, "listen address for metrics and debug server")
housekeeperCmd.Flags().BoolVar(&hkPprofEnabled, "pprof", hkDefaultPprofEnabled, "enable pprof debug endpoints")
}

var housekeeperCmd = &cobra.Command{
Expand Down Expand Up @@ -90,8 +97,8 @@ var housekeeperCmd = &cobra.Command{
DB: db,
BeaconClient: beaconClient,

PprofAPI: hkPprofEnabled,
PprofListenAddress: hkPprofListenAddr,
ListenAddr: hkListenAddr,
PprofAPI: hkPprofEnabled,
}
service := housekeeper.NewHousekeeper(opts)
log.Info("Starting housekeeper service...")
Expand Down
34 changes: 20 additions & 14 deletions services/housekeeper/housekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/flashbots/mev-boost-relay/datastore"
"github.com/flashbots/mev-boost-relay/metrics"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
uberatomic "go.uber.org/atomic"
)
Expand All @@ -34,8 +35,8 @@ type HousekeeperOpts struct {
DB database.IDatabaseService
BeaconClient beaconclient.IMultiBeaconClient

PprofAPI bool
PprofListenAddress string
ListenAddr string
PprofAPI bool
}

type Housekeeper struct {
Expand All @@ -46,8 +47,8 @@ type Housekeeper struct {
db database.IDatabaseService
beaconClient beaconclient.IMultiBeaconClient

pprofAPI bool
pprofListenAddress string
listenAddr string
pprofAPI bool

isStarted uberatomic.Bool
isUpdatingProposerDuties uberatomic.Bool
Expand All @@ -67,8 +68,8 @@ func NewHousekeeper(opts *HousekeeperOpts) *Housekeeper {
redis: opts.Redis,
db: opts.DB,
beaconClient: opts.BeaconClient,
listenAddr: opts.ListenAddr,
pprofAPI: opts.PprofAPI,
pprofListenAddress: opts.PprofListenAddress,
proposersAlreadySaved: make(map[uint64]string),
}

Expand All @@ -93,9 +94,9 @@ func (hk *Housekeeper) Start() (err error) {
return err
}

// Start pprof API, if requested
if hk.pprofAPI {
go hk.startPprofAPI()
// Start HTTP server (metrics + optional pprof)
if hk.listenAddr != "" {
go hk.startHTTPServer()
}

// Start initial tasks
Expand All @@ -113,17 +114,22 @@ func (hk *Housekeeper) Start() (err error) {
}
}

func (hk *Housekeeper) startPprofAPI() {
func (hk *Housekeeper) startHTTPServer() {
r := mux.NewRouter()
hk.log.Infof("Starting pprof API at %s", hk.pprofListenAddress)
r.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
r.Handle("/metrics", promhttp.Handler()).Methods(http.MethodGet)
if hk.pprofAPI {
hk.log.Info("pprof API enabled")
r.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
}

hk.log.Infof("HTTP server listening on %s", hk.listenAddr)
srv := http.Server{ //nolint:gosec
Addr: hk.pprofListenAddress,
Addr: hk.listenAddr,
Handler: r,
}
err := srv.ListenAndServe()
if err != nil {
hk.log.WithError(err).Error("failed to start pprof API")
if err != nil && !errors.Is(err, http.ErrServerClosed) {
hk.log.WithError(err).Error("failed to start HTTP server")
}
}

Expand Down