The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/p2p/stream/common/streammanager/cooldown.go

46 lines
847 B

package streammanager
import (
"container/list"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/whyrusleeping/timecache"
)
const (
coolDownPeriod = 1 * time.Minute
)
type coolDownCache struct {
mu sync.Mutex
timeCache *timecache.TimeCache
}
func newCoolDownCache() *coolDownCache {
tl := timecache.NewTimeCache(coolDownPeriod)
return &coolDownCache{
timeCache: tl,
}
}
// Has check and add the peer ID to the cache
func (cache *coolDownCache) Has(id peer.ID) bool {
cache.mu.Lock()
defer cache.mu.Unlock()
has := cache.timeCache.Has(string(id))
if !has {
cache.timeCache.Add(string(id))
}
return has
}
// Reset the cool down cache
func (cache *coolDownCache) Reset() {
cache.mu.Lock()
defer cache.mu.Unlock()
cache.timeCache.Q = list.New()
cache.timeCache.M = make(map[string]time.Time)
}