From 44fab9eab54718b2fa8b3743095d0fa92a833852 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Fri, 24 Dec 2021 15:46:04 -0500 Subject: [PATCH] reduce lock contention --- exporter.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/exporter.go b/exporter.go index 37dbfc5..ef1bc6a 100644 --- a/exporter.go +++ b/exporter.go @@ -78,6 +78,7 @@ type AccountTx struct { // Hotspot represents a hotspot type Hotspot struct { Address string + UpdateLock sync.Mutex LastUpdate time.Time RewardsTotal map[string]int } @@ -667,6 +668,11 @@ func (e *Exporter) collectHotspotMetrics(wg *sync.WaitGroup, ch chan<- prometheu continue } + // create the hotspot object if it doesn't exist + if _, ok := account.Hotspots[hotspotData.Address]; !ok { + account.Hotspots[hotspotData.Address] = NewHotspot(hotspotData.Address) + } + // collect hotspot metric requiring extra queries in a new routine wg.Add(4) go e.collectHotspotBlocksDeltaMetrics(wg, ch, account, hotspotData) @@ -791,16 +797,15 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr "rewards_v2", } - // TODO: optimize locks - account.UpdateLock.Lock() - defer account.UpdateLock.Unlock() - hotspot, ok := account.Hotspots[hotspotData.Address] if !ok { - hotspot = NewHotspot(hotspotData.Address) - account.Hotspots[hotspotData.Address] = hotspot + log.Fatalf("BUG: attempted to access hotspot %v before it was created", hotspotData.Address) + return } + hotspot.UpdateLock.Lock() + defer hotspot.UpdateLock.Unlock() + // We want to keep in memory the timestamp of the last activity we // received from the api. We cannot do something naive like [lastscrape, now] // because the api can take a few seconds to sync with the chain and