-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsubscriptions_map.go
More file actions
41 lines (35 loc) · 876 Bytes
/
Copy pathsubscriptions_map.go
File metadata and controls
41 lines (35 loc) · 876 Bytes
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
package gobayeux
import (
"fmt"
"sync"
)
type subscriptionsMap struct {
lock sync.RWMutex
subs map[Channel]chan []Message
}
func newSubscriptionsMap() *subscriptionsMap {
return &subscriptionsMap{subs: make(map[Channel]chan []Message)}
}
func (sm *subscriptionsMap) Add(channel Channel, ms chan []Message) error {
sm.lock.Lock()
defer sm.lock.Unlock()
if _, ok := sm.subs[channel]; !ok {
sm.subs[channel] = ms
return nil
}
return fmt.Errorf("channel '%s' already subscribed", channel)
}
func (sm *subscriptionsMap) Remove(channel Channel) {
sm.lock.Lock()
defer sm.lock.Unlock()
delete(sm.subs, channel)
}
func (sm *subscriptionsMap) Get(channel Channel) (chan []Message, error) {
sm.lock.RLock()
defer sm.lock.RUnlock()
ms, ok := sm.subs[channel]
if !ok {
return nil, fmt.Errorf("channel '%s' has no subscriptions", channel)
}
return ms, nil
}