@@ -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.
158159type 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
@@ -509,6 +512,7 @@ func (u *UniverseStats) populateSyncStatsCache() {
509512
510513 // If this is a test, then we'll just purge the items.
511514 if u .opts .cacheDuration == 0 {
515+ log .Debugf ("nil state cache duration, wiping cache" )
512516 u .statsSnapshot .Store (nil )
513517 return
514518 }
@@ -532,7 +536,10 @@ func (u *UniverseStats) populateSyncStatsCache() {
532536
533537 // Reset the timer so we'll refresh again after the cache duration.
534538 if ! u .statsRefresh .Stop () {
535- <- u .statsRefresh .C
539+ select {
540+ case <- u .statsRefresh .C :
541+ default :
542+ }
536543 }
537544
538545 u .statsRefresh .Reset (u .opts .cacheDuration )
@@ -562,6 +569,8 @@ func (u *UniverseStats) AggregateSyncStats(
562569
563570 u .statsCacheLogger .Miss ()
564571
572+ log .Debugf ("Populating aggregate sync stats" )
573+
565574 dbStats , err := u .querySyncStats (ctx )
566575 if err != nil {
567576 return dbStats , err
@@ -571,6 +580,13 @@ func (u *UniverseStats) AggregateSyncStats(
571580 // the stats pointer so we'll refresh it after a period of time.
572581 u .statsSnapshot .Store (& dbStats )
573582
583+ // Reset the timer so we'll refresh again after the cache duration.
584+ if u .statsRefresh != nil && ! u .statsRefresh .Stop () {
585+ select {
586+ case <- u .statsRefresh .C :
587+ default :
588+ }
589+ }
574590 u .statsRefresh = time .AfterFunc (
575591 u .opts .cacheDuration , u .populateSyncStatsCache ,
576592 )
@@ -837,6 +853,16 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context,
837853
838854 // Finally, we'll create the time after function that'll wipe the
839855 // cache, forcing a refresh.
856+ //
857+ // If we already have a timer active, then stop it, so we only have a
858+ // single timer going at any given time.
859+ if u .syncStatsRefresh != nil && ! u .syncStatsRefresh .Stop () {
860+ select {
861+ case <- u .syncStatsRefresh .C :
862+ default :
863+ }
864+ }
865+
840866 u .syncStatsRefresh = time .AfterFunc (u .opts .cacheDuration , func () {
841867 log .Infof ("Purging sync stats cache, duration=%v" ,
842868 u .opts .cacheDuration )
0 commit comments