-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblacklist.go
42 lines (34 loc) · 849 Bytes
/
blacklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package p2p
import (
lru "github.com/hashicorp/golang-lru"
peer "github.com/libp2p/go-libp2p-peer"
)
// LRUBlacklist is a blacklist implementation using an LRU cache
type LRUBlacklist struct {
lru *lru.Cache
}
// NewLRUBlacklist creates a new LRUBlacklist with capacity cap
func NewLRUBlacklist(cap int) (*LRUBlacklist, error) {
c, err := lru.New(cap)
if err != nil {
return nil, err
}
b := &LRUBlacklist{lru: c}
return b, nil
}
// Add adds a peer ID
func (b *LRUBlacklist) Add(p peer.ID) {
b.lru.Add(p, nil)
}
// Remove removes a peer ID
func (b *LRUBlacklist) Remove(p peer.ID) {
b.lru.Remove(p)
}
// RemoveOldest removes the oldest peer ID
func (b *LRUBlacklist) RemoveOldest() {
b.lru.RemoveOldest()
}
// Contains checks if the peer ID is in LRU
func (b *LRUBlacklist) Contains(p peer.ID) bool {
return b.lru.Contains(p)
}