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.
39 lines
723 B
39 lines
723 B
3 years ago
|
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))
|
||
|
cache.timeCache.Add(string(id))
|
||
|
return has
|
||
|
}
|
||
|
|
||
|
// Reset reset the cooldown cache
|
||
|
func (cache *coolDownCache) Reset() {
|
||
|
cache.timeCache.Q = list.New()
|
||
|
cache.timeCache.M = make(map[string]time.Time)
|
||
|
}
|