1
0
Fork 0

reduce lock contention

This commit is contained in:
Massaki Archambault 2021-12-24 15:46:04 -05:00
parent a11c005492
commit 44fab9eab5
1 changed files with 11 additions and 6 deletions

View File

@ -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