[blacklist] Use .Has check before add in time cached version - adjust signature

This commit is contained in:
Edgar Aroutiounian 2020-05-16 07:14:13 -07:00 committed by vyzo
parent 9de0241df1
commit eef4d41e0e
1 changed files with 11 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import (
// Blacklist is an interface for peer blacklisting.
type Blacklist interface {
Add(peer.ID)
Add(peer.ID) bool
Contains(peer.ID) bool
}
@ -22,8 +22,9 @@ func NewMapBlacklist() Blacklist {
return MapBlacklist(make(map[peer.ID]struct{}))
}
func (b MapBlacklist) Add(p peer.ID) {
func (b MapBlacklist) Add(p peer.ID) bool {
b[p] = struct{}{}
return true
}
func (b MapBlacklist) Contains(p peer.ID) bool {
@ -43,11 +44,16 @@ func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) {
return b, nil
}
func (b *TimeCachedBlacklist) Add(p peer.ID) {
// Add returns a bool saying whether Add of peer was successful
func (b *TimeCachedBlacklist) Add(p peer.ID) bool {
b.Lock()
defer b.Unlock()
b.tc.Add(p.String())
s := p.String()
if b.tc.Has(s) {
return false
}
b.tc.Add(s)
return true
}
func (b *TimeCachedBlacklist) Contains(p peer.ID) bool {