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

40 lines
733 B

package streammanager
import (
"container/list"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/whyrusleeping/timecache"
)
const (
coolDownPeriod = 1 * time.Minute
)
type coolDownCache struct {
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 {
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.timeCache.Q = list.New()
cache.timeCache.M = make(map[string]time.Time)
}