metrics: spin up meter ticker routine when enabling metric system (#31400)

Addresses https://github.com/ethereum/go-ethereum/issues/31244
This commit is contained in:
Mike Weyandt 2025-03-16 21:48:08 -04:00 committed by GitHub
parent 7d99f7df00
commit d25c3c32b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View file

@ -131,19 +131,15 @@ var arbiter = meterTicker{meters: make(map[*Meter]struct{})}
type meterTicker struct {
mu sync.RWMutex
started bool
meters map[*Meter]struct{}
once sync.Once
meters map[*Meter]struct{}
}
// add adds another *Meter ot the arbiter, and starts the arbiter ticker.
// add a *Meter to the arbiter
func (ma *meterTicker) add(m *Meter) {
ma.mu.Lock()
defer ma.mu.Unlock()
ma.meters[m] = struct{}{}
if !ma.started {
ma.started = true
go ma.loop()
}
}
// remove removes a meter from the set of ticked meters.
@ -153,7 +149,7 @@ func (ma *meterTicker) remove(m *Meter) {
ma.mu.Unlock()
}
// loop ticks meters on a 5 second interval.
// loop ticks meters on a 5-second interval.
func (ma *meterTicker) loop() {
ticker := time.NewTicker(5 * time.Second)
for range ticker.C {
@ -167,3 +163,8 @@ func (ma *meterTicker) loop() {
ma.mu.RUnlock()
}
}
// startMeterTickerLoop will start the arbiter ticker.
func startMeterTickerLoop() {
arbiter.once.Do(func() { go arbiter.loop() })
}

View file

@ -30,6 +30,7 @@ func Enabled() bool {
// the program, before any metrics collection will happen.
func Enable() {
metricsEnabled = true
startMeterTickerLoop()
}
var threadCreateProfile = pprof.Lookup("threadcreate")