-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribers.go
73 lines (62 loc) · 1.77 KB
/
subscribers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package pts
import "sync"
type ChannelSubscribers struct {
subscribers map[string]*Context
mutex sync.RWMutex
}
func createKey(clientId string, path string) string {
return clientId + "__" + path
}
func (subs *ChannelSubscribers) init() {
subs.subscribers = map[string]*Context{}
}
func (subs *ChannelSubscribers) IsSubscribed(clientId string, path string) bool {
subs.mutex.Lock()
defer subs.mutex.Unlock()
var _, exists = subs.subscribers[createKey(clientId, path)]
return exists
}
func (subs *ChannelSubscribers) GetContext(clientId string, path string) (*Context, bool) {
subs.mutex.Lock()
defer subs.mutex.Unlock()
var context, exists = subs.subscribers[createKey(clientId, path)]
return context, exists
}
func (subs *ChannelSubscribers) Add(context *Context) {
subs.mutex.Lock()
defer subs.mutex.Unlock()
subs.subscribers[createKey(context.Client.Id, context.FullPath)] = context
}
func (subs *ChannelSubscribers) GetAll() []*Context {
var found []*Context
for _, context := range subs.subscribers {
found = append(found, context)
}
return found
}
func (subs *ChannelSubscribers) GetAllForPath(path string) []*Context {
var found []*Context
for _, context := range subs.subscribers {
if context.FullPath == path {
found = append(found, context)
}
}
return found
}
func (subs *ChannelSubscribers) RemoveAllPaths(clientId string) []*Context {
subs.mutex.Lock()
defer subs.mutex.Unlock()
var removed []*Context
for key, context := range subs.subscribers {
if context.Client.Id == clientId {
delete(subs.subscribers, key)
removed = append(removed, context)
}
}
return removed
}
func (subs *ChannelSubscribers) Remove(clientId string, path string) {
subs.mutex.Lock()
defer subs.mutex.Unlock()
delete(subs.subscribers, createKey(clientId, path))
}