-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try own ttl map implementation for discovery
- Loading branch information
Showing
5 changed files
with
668 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ttl | ||
|
||
import ( | ||
"github.com/ssvlabs/ssv/utils/hashmap" | ||
"time" | ||
) | ||
|
||
// Map implements a thread-safe map with a sync.Map under the hood. | ||
type Map[Key comparable, Value any] struct { | ||
*hashmap.Map[Key, Value] | ||
idxLastUpdatedAt hashmap.Map[Key, time.Time] | ||
} | ||
|
||
func New[Key comparable, Value any](lifespan, cleanupInterval time.Duration) *Map[Key, Value] { | ||
m := &Map[Key, Value]{ | ||
Map: hashmap.New[Key, Value](), | ||
idxLastUpdatedAt: hashmap.Map[Key, time.Time]{}, | ||
} | ||
go func() { | ||
// TODO: use time.After when Go is updated to 1.23 | ||
timer := time.NewTicker(cleanupInterval) | ||
defer timer.Stop() | ||
|
||
// TODO - consider terminating with ctx.Done() to make this ttl map garbage-collectable | ||
for range timer.C { | ||
m.idxLastUpdatedAt.Range(func(key Key, t time.Time) bool { | ||
if time.Since(t) > lifespan { | ||
m.idxLastUpdatedAt.Delete(key) | ||
m.Map.Delete(key) | ||
} | ||
return true | ||
}) | ||
} | ||
}() | ||
return m | ||
} | ||
|
||
func (m *Map[Key, Value]) GetOrSet(key Key, value Value) (Value, bool) { | ||
result, loaded := m.Map.GetOrSet(key, value) | ||
if !loaded { | ||
// gotta update timestamp sine we've just set value for this key | ||
m.idxLastUpdatedAt.Set(key, time.Now()) | ||
} | ||
return result, loaded | ||
} | ||
|
||
func (m *Map[Key, Value]) CompareAndSwap(key Key, old, new Value) (swapped bool) { | ||
swapped = m.Map.CompareAndSwap(key, old, new) | ||
if swapped { | ||
// gotta update timestamp sine we've just set value for this key | ||
m.idxLastUpdatedAt.Set(key, time.Now()) | ||
} | ||
return swapped | ||
} | ||
|
||
func (m *Map[Key, Value]) Set(key Key, value Value) { | ||
m.Map.Set(key, value) | ||
m.idxLastUpdatedAt.Set(key, time.Now()) | ||
} |
Oops, something went wrong.