-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
51 lines (41 loc) · 987 Bytes
/
session.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
package sprout
import (
"sync"
"git.sr.ht/~whereswaldon/forest-go/fields"
)
// Session stores the state of a sprout connection between hosts,
// which is currently just the subscribed community set.
type Session struct {
sync.RWMutex
Communities map[*fields.QualifiedHash]struct{}
}
func NewSession() *Session {
return &Session{
Communities: make(map[*fields.QualifiedHash]struct{}),
}
}
func (c *Session) Subscribe(communityID *fields.QualifiedHash) {
c.Lock()
defer c.Unlock()
c.Communities[communityID] = struct{}{}
}
func (c *Session) IsSubscribed(communityID *fields.QualifiedHash) bool {
c.RLock()
defer c.RUnlock()
for community := range c.Communities {
if community.Equals(communityID) {
return true
}
}
return false
}
func (c *Session) Unsubscribe(communityID *fields.QualifiedHash) {
c.Lock()
defer c.Unlock()
for community := range c.Communities {
if community.Equals(communityID) {
delete(c.Communities, community)
return
}
}
}