From 0b43e3f85359a1af3517803f415c674fe3853f63 Mon Sep 17 00:00:00 2001 From: Jason Brill Date: Fri, 8 Nov 2024 10:57:40 -0500 Subject: [PATCH] tapdb: universe lock caching optimization --- tapdb/universe_stats.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tapdb/universe_stats.go b/tapdb/universe_stats.go index 709355931..df8aeaedd 100644 --- a/tapdb/universe_stats.go +++ b/tapdb/universe_stats.go @@ -290,11 +290,11 @@ type UniverseStats struct { statsCacheLogger *cacheLogger statsRefresh *time.Timer - eventsMtx sync.Mutex + eventsMtx sync.RWMutex assetEventsCache assetEventsCache eventsCacheLogger *cacheLogger - syncStatsMtx sync.Mutex + syncStatsMtx sync.RWMutex syncStatsCache *atomicSyncStatsCache syncStatsRefresh *time.Timer } @@ -635,7 +635,9 @@ func (u *UniverseStats) QueryAssetStatsPerDay(ctx context.Context, // First, we'll check to see if we already have a cached result for // this query. query := newEventQuery(q) + u.eventsMtx.RLock() cachedResult, err := u.assetEventsCache.Get(query) + u.eventsMtx.RUnlock() if err == nil { u.eventsCacheLogger.Hit() return cachedResult, nil @@ -750,13 +752,18 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context, // First, check the cache to see if we already have a cached result for // this query. - syncSnapshots := u.syncStatsCache.fetchQuery(q) + var syncSnapshots []universe.AssetSyncSnapshot + + u.syncStatsMtx.RLock() + syncSnapshots = u.syncStatsCache.fetchQuery(q) + u.syncStatsMtx.RUnlock() + if syncSnapshots != nil { resp.SyncStats = syncSnapshots return resp, nil } - // Otherwise, we'll grab the main mutex so we can qury the db then + // Otherwise, we'll grab the main mutex so we can query the db then // cache the result. u.syncStatsMtx.Lock() defer u.syncStatsMtx.Unlock()