-
Notifications
You must be signed in to change notification settings - Fork 217
/
pubsub.go
240 lines (205 loc) · 4.92 KB
/
pubsub.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package miniredis
import (
"regexp"
"sort"
"sync"
"github.com/alicebob/miniredis/v2/server"
)
// PubsubMessage is what gets broadcasted over pubsub channels.
type PubsubMessage struct {
Channel string
Message string
}
type PubsubPmessage struct {
Pattern string
Channel string
Message string
}
// Subscriber has the (p)subscriptions.
type Subscriber struct {
publish chan PubsubMessage
ppublish chan PubsubPmessage
channels map[string]struct{}
patterns map[string]*regexp.Regexp
mu sync.Mutex
}
// Make a new subscriber. The channel is not buffered, so you will need to keep
// reading using Messages(). Use Close() when done, or unsubscribe.
func newSubscriber() *Subscriber {
return &Subscriber{
publish: make(chan PubsubMessage),
ppublish: make(chan PubsubPmessage),
channels: map[string]struct{}{},
patterns: map[string]*regexp.Regexp{},
}
}
// Close the listening channel
func (s *Subscriber) Close() {
close(s.publish)
close(s.ppublish)
}
// Count the total number of channels and patterns
func (s *Subscriber) Count() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.count()
}
func (s *Subscriber) count() int {
return len(s.channels) + len(s.patterns)
}
// Subscribe to a channel. Returns the total number of (p)subscriptions after
// subscribing.
func (s *Subscriber) Subscribe(c string) int {
s.mu.Lock()
defer s.mu.Unlock()
s.channels[c] = struct{}{}
return s.count()
}
// Unsubscribe a channel. Returns the total number of (p)subscriptions after
// unsubscribing.
func (s *Subscriber) Unsubscribe(c string) int {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.channels, c)
return s.count()
}
// Subscribe to a pattern. Returns the total number of (p)subscriptions after
// subscribing.
func (s *Subscriber) Psubscribe(pat string) int {
s.mu.Lock()
defer s.mu.Unlock()
s.patterns[pat] = patternRE(pat)
return s.count()
}
// Unsubscribe a pattern. Returns the total number of (p)subscriptions after
// unsubscribing.
func (s *Subscriber) Punsubscribe(pat string) int {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.patterns, pat)
return s.count()
}
// List all subscribed channels, in alphabetical order
func (s *Subscriber) Channels() []string {
s.mu.Lock()
defer s.mu.Unlock()
var cs []string
for c := range s.channels {
cs = append(cs, c)
}
sort.Strings(cs)
return cs
}
// List all subscribed patterns, in alphabetical order
func (s *Subscriber) Patterns() []string {
s.mu.Lock()
defer s.mu.Unlock()
var ps []string
for p := range s.patterns {
ps = append(ps, p)
}
sort.Strings(ps)
return ps
}
// Publish a message. Will return return how often we sent the message (can be
// a match for a subscription and for a psubscription.
func (s *Subscriber) Publish(c, msg string) int {
s.mu.Lock()
defer s.mu.Unlock()
found := 0
subs:
for sub := range s.channels {
if sub == c {
s.publish <- PubsubMessage{c, msg}
found++
break subs
}
}
pats:
for orig, pat := range s.patterns {
if pat != nil && pat.MatchString(c) {
s.ppublish <- PubsubPmessage{orig, c, msg}
found++
break pats
}
}
return found
}
// The channel to read messages for this subscriber. Only for messages matching
// a SUBSCRIBE.
func (s *Subscriber) Messages() <-chan PubsubMessage {
return s.publish
}
// The channel to read messages for this subscriber. Only for messages matching
// a PSUBSCRIBE.
func (s *Subscriber) Pmessages() <-chan PubsubPmessage {
return s.ppublish
}
// List all pubsub channels. If `pat` isn't empty channels names must match the
// pattern. Channels are returned alphabetically.
func activeChannels(subs []*Subscriber, pat string) []string {
channels := map[string]struct{}{}
for _, s := range subs {
for c := range s.channels {
channels[c] = struct{}{}
}
}
var cpat *regexp.Regexp
if pat != "" {
cpat = patternRE(pat)
}
var cs []string
for k := range channels {
if cpat != nil && !cpat.MatchString(k) {
continue
}
cs = append(cs, k)
}
sort.Strings(cs)
return cs
}
// Count all subscribed (not psubscribed) clients for the given channel
// pattern. Channels are returned alphabetically.
func countSubs(subs []*Subscriber, channel string) int {
n := 0
for _, p := range subs {
for c := range p.channels {
if c == channel {
n++
break
}
}
}
return n
}
// Count the total of all client psubscriptions.
func countPsubs(subs []*Subscriber) int {
n := 0
for _, p := range subs {
n += len(p.patterns)
}
return n
}
func monitorPublish(conn *server.Peer, msgs <-chan PubsubMessage) {
for msg := range msgs {
conn.Block(func(c *server.Writer) {
c.WritePushLen(3)
c.WriteBulk("message")
c.WriteBulk(msg.Channel)
c.WriteBulk(msg.Message)
c.Flush()
})
}
}
func monitorPpublish(conn *server.Peer, msgs <-chan PubsubPmessage) {
for msg := range msgs {
conn.Block(func(c *server.Writer) {
c.WritePushLen(4)
c.WriteBulk("pmessage")
c.WriteBulk(msg.Pattern)
c.WriteBulk(msg.Channel)
c.WriteBulk(msg.Message)
c.Flush()
})
}
}