Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix concurrent rw on map in operation_pool #10140

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading