lib/storage: eliminate data race when updating lastSyncTime in dateMetricIDCache.Has

This commit is contained in:
Aliaksandr Valialkin 2019-11-10 22:03:46 +02:00
parent 7247a7862d
commit 6bdde0d6d4

View file

@ -1020,7 +1020,7 @@ type dateMetricIDCache struct {
// Contains mutable map protected by mu
byDateMutable *byDateMetricIDMap
lastSyncTime time.Time
mu sync.RWMutex
mu sync.Mutex
}
func newDateMetricIDCache() *dateMetricIDCache {
@ -1057,7 +1057,7 @@ func (dmc *dateMetricIDCache) Has(date, metricID uint64) bool {
// Slow path. Check mutable map.
currentTime := time.Now()
dmc.mu.RLock()
dmc.mu.Lock()
v = dmc.byDateMutable.get(date)
ok := v.Has(metricID)
mustSync := false
@ -1065,7 +1065,7 @@ func (dmc *dateMetricIDCache) Has(date, metricID uint64) bool {
mustSync = true
dmc.lastSyncTime = currentTime
}
dmc.mu.RUnlock()
dmc.mu.Unlock()
if mustSync {
dmc.sync()