@@ -25,7 +25,16 @@ type BadgerConnector struct {
2525 gcTicker * time.Ticker
2626 stopGC chan struct {}
2727
28+ // Configuration
29+ stagingDataTTL time.Duration // TTL for staging data entries
30+ gcInterval time.Duration // Interval for running garbage collection
31+ cacheRefreshInterval time.Duration // Interval for refreshing range cache
32+ cacheStalenessTimeout time.Duration // Timeout before considering cache entry stale
33+
2834 // In-memory block range cache
35+ // NOTE: Staging data has a TTL. The cache is refreshed periodically
36+ // to detect expired entries and update min/max ranges accordingly.
37+ // Badger doesn't provide expiry notifications, so we rely on periodic scanning.
2938 rangeCache map [string ]* blockRange // chainId -> range
3039 rangeCacheMu sync.RWMutex
3140 rangeUpdateChan chan string // channel for triggering background updates
@@ -71,15 +80,19 @@ func NewBadgerConnector(cfg *config.BadgerConfig) (*BadgerConnector, error) {
7180 }
7281
7382 bc := & BadgerConnector {
74- db : db ,
75- stopGC : make (chan struct {}),
76- rangeCache : make (map [string ]* blockRange ),
77- rangeUpdateChan : make (chan string , 5 ),
78- stopRangeUpdate : make (chan struct {}),
83+ db : db ,
84+ stopGC : make (chan struct {}),
85+ rangeCache : make (map [string ]* blockRange ),
86+ rangeUpdateChan : make (chan string , 5 ),
87+ stopRangeUpdate : make (chan struct {}),
88+ stagingDataTTL : 10 * time .Minute ,
89+ gcInterval : 60 * time .Second ,
90+ cacheRefreshInterval : 60 * time .Second ,
91+ cacheStalenessTimeout : 120 * time .Second ,
7992 }
8093
8194 // Start GC routine
82- bc .gcTicker = time .NewTicker (time . Duration ( 60 ) * time . Second )
95+ bc .gcTicker = time .NewTicker (bc . gcInterval )
8396 go bc .runGC ()
8497
8598 // Start range cache update routine
@@ -104,7 +117,7 @@ func (bc *BadgerConnector) runGC() {
104117
105118// runRangeCacheUpdater runs in the background to validate cache entries
106119func (bc * BadgerConnector ) runRangeCacheUpdater () {
107- ticker := time .NewTicker (time . Minute )
120+ ticker := time .NewTicker (bc . cacheRefreshInterval )
108121 defer ticker .Stop ()
109122
110123 for {
@@ -184,7 +197,7 @@ func (bc *BadgerConnector) refreshStaleRanges() {
184197 staleChains := []string {}
185198 now := time .Now ()
186199 for chainId , r := range bc .rangeCache {
187- if now .Sub (r .lastUpdated ) > 3 * time . Minute {
200+ if now .Sub (r .lastUpdated ) > bc . cacheStalenessTimeout {
188201 staleChains = append (staleChains , chainId )
189202 }
190203 }
@@ -391,7 +404,9 @@ func (bc *BadgerConnector) InsertStagingData(data []common.BlockData) error {
391404 return err
392405 }
393406
394- if err := txn .Set (key , buf .Bytes ()); err != nil {
407+ // Set with configured TTL for staging data
408+ entry := badger .NewEntry (key , buf .Bytes ()).WithTTL (bc .stagingDataTTL )
409+ if err := txn .SetEntry (entry ); err != nil {
395410 return err
396411 }
397412
0 commit comments