set.go 1.18 KB
Newer Older
1 2 3 4 5 6
package peer

import (
	"sync"
)

Vasco Santos's avatar
Vasco Santos committed
7
// PeerSet is a threadsafe set of peers.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
type Set struct {
	lk sync.RWMutex
	ps map[ID]struct{}

	size int
}

func NewSet() *Set {
	ps := new(Set)
	ps.ps = make(map[ID]struct{})
	ps.size = -1
	return ps
}

func NewLimitedSet(size int) *Set {
	ps := new(Set)
	ps.ps = make(map[ID]struct{})
	ps.size = size
	return ps
}

func (ps *Set) Add(p ID) {
	ps.lk.Lock()
	ps.ps[p] = struct{}{}
	ps.lk.Unlock()
}

func (ps *Set) Contains(p ID) bool {
	ps.lk.RLock()
	_, ok := ps.ps[p]
	ps.lk.RUnlock()
	return ok
}

func (ps *Set) Size() int {
	ps.lk.RLock()
	defer ps.lk.RUnlock()
	return len(ps.ps)
}

// TryAdd Attempts to add the given peer into the set.
// This operation can fail for one of two reasons:
// 1) The given peer is already in the set
// 2) The number of peers in the set is equal to size
func (ps *Set) TryAdd(p ID) bool {
	var success bool
	ps.lk.Lock()
	if _, ok := ps.ps[p]; !ok && (len(ps.ps) < ps.size || ps.size == -1) {
		success = true
		ps.ps[p] = struct{}{}
	}
	ps.lk.Unlock()
	return success
}

func (ps *Set) Peers() []ID {
	ps.lk.Lock()
	out := make([]ID, 0, len(ps.ps))
Marten Seemann's avatar
Marten Seemann committed
66
	for p := range ps.ps {
67 68 69 70 71
		out = append(out, p)
	}
	ps.lk.Unlock()
	return out
}