From d25c3c32b24c5e176d9433b9f8da53eaf8afa95f Mon Sep 17 00:00:00 2001 From: Mike Weyandt Date: Sun, 16 Mar 2025 21:48:08 -0400 Subject: [PATCH] metrics: spin up meter ticker routine when enabling metric system (#31400) Addresses https://github.com/ethereum/go-ethereum/issues/31244 --- metrics/meter.go | 17 +++++++++-------- metrics/metrics.go | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/metrics/meter.go b/metrics/meter.go index 194bd1f304..197e5abbed 100644 --- a/metrics/meter.go +++ b/metrics/meter.go @@ -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() }) +} diff --git a/metrics/metrics.go b/metrics/metrics.go index c4c43b7576..088948d403 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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")