-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
135 lines (124 loc) · 2.77 KB
/
bucket.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package channel
import (
"sync"
)
type bucketOptions struct {
maxLimitSessions int
skipSlowSubscribers bool
bufSize int
}
type bucket struct {
sync.RWMutex
subscribers map[string][]Subscription
queue chan []byte
done chan struct{}
sessionCount uint
subscribersCount uint
options *bucketOptions
}
func newBucket(
opt *bucketOptions,
) *bucket {
b := &bucket{
subscribers: make(map[string][]Subscription),
queue: make(chan []byte, 1),
done: make(chan struct{}),
options: opt,
}
go b.listen()
return b
}
func (b *bucket) listen() {
for {
select {
case <-b.done:
return
case msg := <-b.queue:
b.publish(msg)
}
}
}
func (b *bucket) unsubscribe(subscriber string, session string) error {
b.Lock()
defer b.Unlock()
subscriptions, found := b.subscribers[subscriber]
if !found {
return ErrSubscriptionNotFound
}
for i, subscription := range subscriptions {
if subscription.Equal(session) {
subscriptions = append(subscriptions[:i], subscriptions[i+1:]...)
subscription.Close()
break
}
}
if len(subscriptions) == 0 {
delete(b.subscribers, subscriber)
} else {
b.subscribers[subscriber] = subscriptions
}
b.subscribersCount = uint(len(b.subscribers))
if b.sessionCount > 0 {
b.sessionCount--
}
return nil
}
func (b *bucket) publishTo(subscriber string, payload []byte) error {
b.RLock()
defer b.RUnlock()
subscriptions, found := b.subscribers[subscriber]
if !found {
return ErrSubscriberNotFound
}
for i := len(subscriptions) - 1; i >= 0; i-- {
subscriptions[i].Publish(
payload,
b.options.skipSlowSubscribers,
)
}
return nil
}
func (b *bucket) publish(payload []byte) {
b.RLock()
defer b.RUnlock()
for _, subscriptions := range b.subscribers {
for i := len(subscriptions) - 1; i >= 0; i-- {
subscriptions[i].Publish(
payload,
b.options.skipSlowSubscribers,
)
}
}
}
func (b *bucket) close() {
b.Lock()
defer b.Unlock()
for sid, subscriptions := range b.subscribers {
for i := len(subscriptions) - 1; i >= 0; i-- {
if !subscriptions[i].IsClosed() {
subscriptions[i].Close()
b.sessionCount--
}
}
delete(b.subscribers, sid)
}
b.subscribersCount = uint(len(b.subscribers))
close(b.done)
}
func (b *bucket) subscribe(subscriber string) (Subscription, error) {
b.Lock()
defer b.Unlock()
subscriptions, found := b.subscribers[subscriber]
if !found {
subscriptions = make([]Subscription, 0, 1)
}
if len(subscriptions) >= b.options.maxLimitSessions {
return Subscription{}, ErrTooManySessions
}
s := MakeSubscription(subscriber, b.options.bufSize)
subscriptions = append(subscriptions, s)
b.subscribers[subscriber] = subscriptions
b.subscribersCount = uint(len(b.subscribers))
b.sessionCount++
return s, nil
}