Skip to content

Commit

Permalink
tapdb: universe lock caching optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrill committed Nov 20, 2024
1 parent 2d408d9 commit 4b0a532
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tapdb/universe_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4b0a532

Please sign in to comment.