mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-04 13:53:06 +00:00
* reimplement timecache for sane and performant behaviour * remove seenMessagesMx, take advantage of new tc api * fix timecache tests * fix typo * store expiry, don't make life difficult * refactor common background sweep procedure for both impls * add godocs to TimeCache
36 lines
537 B
Go
36 lines
537 B
Go
package timecache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var backgroundSweepInterval = time.Minute
|
|
|
|
func background(ctx context.Context, lk sync.Locker, m map[string]time.Time) {
|
|
ticker := time.NewTimer(backgroundSweepInterval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case now := <-ticker.C:
|
|
sweep(lk, m, now)
|
|
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func sweep(lk sync.Locker, m map[string]time.Time, now time.Time) {
|
|
lk.Lock()
|
|
defer lk.Unlock()
|
|
|
|
for k, expiry := range m {
|
|
if expiry.Before(now) {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
}
|