/
blocklist.go
52 lines (42 loc) · 1.14 KB
/
blocklist.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
43
44
45
46
47
48
49
50
51
52
/*
Copyright (C) 2017 - 2024 Asynkron.se <http://www.asynkron.se>
*/
package remote
import (
"sync"
"github.com/asynkron/gofun/set"
)
// TODO: document it
type BlockList struct {
mu *sync.RWMutex
blockedMembers *set.ImmutableSet[string]
}
func NewBlockList() *BlockList {
blocklist := BlockList{
mu: &sync.RWMutex{},
blockedMembers: set.NewImmutable[string](),
}
return &blocklist
}
func (bl *BlockList) BlockedMembers() set.Set[string] {
return bl.blockedMembers
}
// Block adds the given memberID list to the BlockList
func (bl *BlockList) Block(memberIDs ...string) {
// acquire our mutual exclusion primitive
bl.mu.Lock()
defer bl.mu.Unlock()
bl.blockedMembers = bl.blockedMembers.AddRange(memberIDs...)
}
// IsBlocked returns true if the given memberID string has been
// ever added to the BlockList
func (bl *BlockList) IsBlocked(memberID string) bool {
// acquire our mutual exclusion primitive for reading
return bl.blockedMembers.Contains(memberID)
}
// Len returns the number of blocked members
func (bl *BlockList) Len() int {
bl.mu.RLock()
defer bl.mu.RUnlock()
return bl.blockedMembers.Size()
}