Skip to content

Commit 9ab2d30

Browse files
committed
tapdb: ensure universe asset stats ticker is stopped before refresh
In this commit, we fix a bug that would cause the universe stats cache to be wiped more than once every 30 minutes. Before this commit, we'd accumulate a new background `time.AfterFunc` ticker for each unique request set in the cache. To fix this, we'll always first stop the old ticker before making a new one.
1 parent e6b0516 commit 9ab2d30

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

tapdb/multiverse.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ func (r *rootNodeCache) fetchRoots(q universe.RootNodesQuery,
275275
defer r.RUnlock()
276276
}
277277

278-
log.Infof("checking cache for roots")
279-
280278
// Attempt to read directly from the root node cache.
281279
rootNodeCache := r.allRoots.Load()
282280
rootNodes, _ := rootNodeCache.Get(newRootPageQuery(q))
@@ -650,7 +648,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
650648
// Attempt to read directly from the root node cache.
651649
rootNodes := b.rootNodeCache.fetchRoots(q, false)
652650
if len(rootNodes) > 0 {
653-
log.Debugf("read %d root nodes from cache", len(rootNodes))
651+
log.Tracef("read %d root nodes from cache", len(rootNodes))
654652
return rootNodes, nil
655653
}
656654

@@ -661,7 +659,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
661659
// the mutex.
662660
rootNodes = b.rootNodeCache.fetchRoots(q, true)
663661
if len(rootNodes) > 0 {
664-
log.Debugf("read %d root nodes from cache", len(rootNodes))
662+
log.Tracef("read %d root nodes from cache", len(rootNodes))
665663
return rootNodes, nil
666664
}
667665

tapdb/universe_stats.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
1414
"github.com/btcsuite/btcd/btcec/v2/schnorr"
15+
"github.com/davecgh/go-spew/spew"
1516
"github.com/lightninglabs/neutrino/cache/lru"
1617
"github.com/lightninglabs/taproot-assets/asset"
1718
"github.com/lightninglabs/taproot-assets/fn"
@@ -152,7 +153,7 @@ type assetEventsCache = *lru.Cache[eventQuery, cachedAssetEvents]
152153

153154
// statsQueryCacheSize is the total number of asset query responses that we'll
154155
// hold inside the cache.
155-
const statsQueryCacheSize = 80_0000
156+
const statsQueryCacheSize = 80_000
156157

157158
// cachedSyncStats is a cached set of sync stats.
158159
type cachedSyncStats []universe.AssetSyncSnapshot
@@ -266,6 +267,8 @@ func (a *atomicSyncStatsCache) storeQuery(q universe.SyncStatsQuery,
266267

267268
statsCache := a.Load()
268269

270+
log.Debugf("Storing asset stats query: %v", spew.Sdump(q))
271+
269272
_, _ = statsCache.Put(query, cachedSyncStats(resp))
270273
}
271274

@@ -837,6 +840,13 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context,
837840

838841
// Finally, we'll create the time after function that'll wipe the
839842
// cache, forcing a refresh.
843+
//
844+
// If we already have a timer active, then stop it, so we only have a
845+
// single timer going at any given time.
846+
if u.syncStatsRefresh != nil && !u.syncStatsRefresh.Stop() {
847+
<-u.syncStatsRefresh.C
848+
}
849+
840850
u.syncStatsRefresh = time.AfterFunc(u.opts.cacheDuration, func() {
841851
log.Infof("Purging sync stats cache, duration=%v",
842852
u.opts.cacheDuration)

0 commit comments

Comments
 (0)