2015-10-25 18:21:51 -07:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IPeerSet has a (immutable) subset of the methods of PeerSet.
|
|
|
|
type IPeerSet interface {
|
|
|
|
Has(key string) bool
|
|
|
|
Get(key string) *Peer
|
|
|
|
List() []*Peer
|
|
|
|
Size() int
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// PeerSet is a special structure for keeping a table of peers.
|
|
|
|
// Iteration over the peers is super fast and thread-safe.
|
|
|
|
type PeerSet struct {
|
2015-12-05 09:44:03 -08:00
|
|
|
mtx sync.Mutex
|
|
|
|
lookup map[string]*peerSetItem
|
|
|
|
list []*Peer
|
2015-10-25 18:21:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type peerSetItem struct {
|
|
|
|
peer *Peer
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// NewPeerSet creates a new peerSet with a list of initial capacity of 256 items.
|
2015-10-25 18:21:51 -07:00
|
|
|
func NewPeerSet() *PeerSet {
|
|
|
|
return &PeerSet{
|
2015-12-05 09:44:03 -08:00
|
|
|
lookup: make(map[string]*peerSetItem),
|
|
|
|
list: make([]*Peer, 0, 256),
|
2015-10-25 18:21:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Add returns false if the peer's Key (PubKeyEd25519) is already memoized.
|
|
|
|
// If the peer was already added, it returns ErrSwitchDuplicatePeer.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) Add(peer *Peer) error {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
defer ps.mtx.Unlock()
|
|
|
|
if ps.lookup[peer.Key] != nil {
|
|
|
|
return ErrSwitchDuplicatePeer
|
|
|
|
}
|
|
|
|
|
|
|
|
index := len(ps.list)
|
|
|
|
// Appending is safe even with other goroutines
|
|
|
|
// iterating over the ps.list slice.
|
|
|
|
ps.list = append(ps.list, peer)
|
|
|
|
ps.lookup[peer.Key] = &peerSetItem{peer, index}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Has returns true iff the peerset contains
|
|
|
|
// the peer referred to by this peerKey.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) Has(peerKey string) bool {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
_, ok := ps.lookup[peerKey]
|
2017-08-25 18:35:00 -07:00
|
|
|
ps.mtx.Unlock()
|
2015-10-25 18:21:51 -07:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Get looks up a peer by the provided peerKey.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) Get(peerKey string) *Peer {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
defer ps.mtx.Unlock()
|
|
|
|
item, ok := ps.lookup[peerKey]
|
|
|
|
if ok {
|
|
|
|
return item.peer
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Remove discards peer by its Key, if the peer was previously memoized.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) Remove(peer *Peer) {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
defer ps.mtx.Unlock()
|
|
|
|
item := ps.lookup[peer.Key]
|
|
|
|
if item == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := item.index
|
2017-08-25 18:35:00 -07:00
|
|
|
// Create a new copy of the list but with one less item.
|
|
|
|
// (we must copy because we'll be mutating the list).
|
2015-10-25 18:21:51 -07:00
|
|
|
newList := make([]*Peer, len(ps.list)-1)
|
|
|
|
copy(newList, ps.list)
|
|
|
|
// If it's the last peer, that's an easy special case.
|
|
|
|
if index == len(ps.list)-1 {
|
|
|
|
ps.list = newList
|
|
|
|
delete(ps.lookup, peer.Key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Replace the popped item with the last item in the old list.
|
2015-10-25 18:21:51 -07:00
|
|
|
lastPeer := ps.list[len(ps.list)-1]
|
|
|
|
lastPeerKey := lastPeer.Key
|
|
|
|
lastPeerItem := ps.lookup[lastPeerKey]
|
|
|
|
newList[index] = lastPeer
|
|
|
|
lastPeerItem.index = index
|
|
|
|
ps.list = newList
|
|
|
|
delete(ps.lookup, peer.Key)
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// Size returns the number of unique items in the peerSet.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) Size() int {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
defer ps.mtx.Unlock()
|
|
|
|
return len(ps.list)
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:35:00 -07:00
|
|
|
// List returns the threadsafe list of peers.
|
2015-10-25 18:21:51 -07:00
|
|
|
func (ps *PeerSet) List() []*Peer {
|
|
|
|
ps.mtx.Lock()
|
|
|
|
defer ps.mtx.Unlock()
|
|
|
|
return ps.list
|
|
|
|
}
|