diff --git a/tsdb/engine/tsm1/cache.go b/tsdb/engine/tsm1/cache.go index 5bd3b86d4a..a8cd2eda39 100644 --- a/tsdb/engine/tsm1/cache.go +++ b/tsdb/engine/tsm1/cache.go @@ -103,24 +103,26 @@ func (c *Cache) Write(key string, values []Value) error { // WriteMulti writes the map of keys and associated values to the cache. This function is goroutine-safe. // It returns an error if the cache has exceeded its max size. func (c *Cache) WriteMulti(values map[string][]Value) error { - c.mu.Lock() - defer c.mu.Unlock() - totalSz := 0 for _, v := range values { totalSz += Values(v).Size() } // Enough room in the cache? + c.mu.RLock() newSize := c.size + uint64(totalSz) if c.maxSize > 0 && newSize+c.snapshotsSize > c.maxSize { + c.mu.RUnlock() return ErrCacheMemoryExceeded } + c.mu.RUnlock() + c.mu.Lock() for k, v := range values { c.write(k, v) } c.size = newSize + c.mu.Unlock() return nil }