Skip to content

Commit

Permalink
fix concurrent rw on map in operation_pool (#10140)
Browse files Browse the repository at this point in the history
relates to #10139
  • Loading branch information
domiwei authored Apr 30, 2024
1 parent 70dfadd commit 01e4969
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions cl/pool/operation_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pool

import (
"sync"
"time"

"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
Expand All @@ -12,7 +13,7 @@ var operationsMultiplier = 20 // Cap the amount of cached element to max_operati

type OperationPool[K comparable, T any] struct {
pool *lru.Cache[K, T] // Map the Signature to the underlying object
recentlySeen map[K]time.Time
recentlySeen sync.Map // map from K to time.Time
lastPruned time.Time
}

Expand All @@ -23,26 +24,23 @@ func NewOperationPool[K comparable, T any](maxOperationsPerBlock int, matricName
}
return &OperationPool[K, T]{
pool: pool,
recentlySeen: make(map[K]time.Time),
recentlySeen: sync.Map{},
}
}

func (o *OperationPool[K, T]) Insert(k K, operation T) {
if _, ok := o.recentlySeen[k]; ok {
if _, ok := o.recentlySeen.Load(k); ok {
return
}
o.pool.Add(k, operation)
o.recentlySeen[k] = time.Now()
o.recentlySeen.Store(k, time.Now())
if time.Since(o.lastPruned) > lifeSpan {
deleteList := make([]K, 0, len(o.recentlySeen))
for k, t := range o.recentlySeen {
if time.Since(t) > lifeSpan {
deleteList = append(deleteList, k)
o.recentlySeen.Range(func(k, v interface{}) bool {
if time.Since(v.(time.Time)) > lifeSpan {
o.recentlySeen.Delete(k)
}
}
for _, k := range deleteList {
delete(o.recentlySeen, k)
}
return true
})
o.lastPruned = time.Now()
}
}
Expand Down

0 comments on commit 01e4969

Please sign in to comment.