/
pubsub.go
439 lines (363 loc) · 8.83 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2013, Chandra Sekar S. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the README.md file.
// Package pubsub implements a simple multi-topic pub-sub
// library.
//
// Topics must be strings and messages of any type can be
// published. A topic can have any number of subscribers and
// all of them receive messages published on the topic.
package pubsub
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
)
type operation int
var ErrorCongestionMsgLost = errors.New("PubSub Congestion: message lost, try again")
const (
sub operation = iota
subOnce
subAdd
unsub
)
type pubOptions struct {
retain bool
}
type PubOption func(*pubOptions)
func WithRetain() PubOption {
return func(options *pubOptions) {
options.retain = true
}
}
type Publisher interface {
Pub(msg interface{}, topics []string, opts ...PubOption)
TryPub(msg interface{}, topics []string, opts ...PubOption) bool
}
type Subscriber interface {
Sub(topics ...string) <-chan interface{}
SubOnce(topics ...string) <-chan interface{}
AddTopics(ch <-chan interface{}, topics ...string)
Unsub(ch <-chan interface{}, topics ...string)
}
type PubSub interface {
Publisher
Subscriber
Shutdown()
}
// pubSub is a collection of topics.
type pubSub struct {
ctx context.Context
cancel context.CancelFunc
group *errgroup.Group
cmdch chan cmd
msgch chan msgEnvelope
topics topics
revTopics revTopics
capacity int
}
type topicSubs map[<-chan interface{}]*subscriber
type topic struct {
retained interface{}
subs topicSubs
}
type revSubscriber struct {
sub *subscriber
topics map[string]bool
}
type topics map[string]*topic
type revTopics map[<-chan interface{}]*revSubscriber
type cmd struct {
op operation
topics []string
ch interface{}
done chan<- bool
}
type msgEnvelope struct {
topics []string
done chan<- bool
msg interface{}
opts pubOptions
}
type subscriber struct {
ctx context.Context
och chan interface{}
ich chan interface{}
done chan struct{}
cmdch chan<- cmd
once bool
}
// New creates a new PubSub and starts a goroutine for handling operations.
// The capacity of the channels created by Sub and SubOnce will be as specified.
// The default is the blocking variant, since this is the original behaviour
func New(ctx context.Context, capacity int) PubSub {
ctx, cancel := context.WithCancel(ctx)
group, ctx := errgroup.WithContext(ctx)
ps := &pubSub{
ctx: ctx,
cancel: cancel,
group: group,
cmdch: make(chan cmd),
msgch: make(chan msgEnvelope, capacity),
topics: make(topics),
revTopics: make(revTopics),
capacity: capacity,
}
ps.group.Go(ps.run)
return ps
}
// Sub returns a channel on which messages published on any of
// the specified topics can be received.
func (ps *pubSub) Sub(topics ...string) <-chan interface{} {
return ps.sub(sub, topics...)
}
// SubOnce is similar to Sub, but only the first message published, after subscription,
// on any of the specified topics can be received.
// Note that if you DO NOT CLEAR the channel in time, ALL PUBSUBs will be blocked
func (ps *pubSub) SubOnce(topics ...string) <-chan interface{} {
return ps.sub(subOnce, topics...)
}
func (ps *pubSub) sub(op operation, topics ...string) <-chan interface{} {
done := make(chan bool, 1)
ch := make(chan interface{}, ps.capacity)
select {
case <-ps.ctx.Done():
case ps.cmdch <- cmd{op: op, topics: topics, ch: ch, done: done}:
}
select {
case <-ps.ctx.Done():
case <-done:
}
return ch
}
// AddTopics adds subscriptions to an existing channel.
func (ps *pubSub) AddTopics(ch <-chan interface{}, topics ...string) {
done := make(chan bool, 1)
select {
case <-ps.ctx.Done():
case ps.cmdch <- cmd{op: subAdd, topics: topics, ch: ch, done: done}:
}
select {
case <-ps.ctx.Done():
case <-done:
}
}
// Pub publishes the given message to all subscribers of the specified topics.
func (ps *pubSub) Pub(msg interface{}, topics []string, opts ...PubOption) {
done := make(chan bool, 1)
op := pubOptions{}
for _, opt := range opts {
opt(&op)
}
select {
case <-ps.ctx.Done():
case ps.msgch <- msgEnvelope{topics: topics, done: done, msg: msg, opts: op}:
}
select {
case <-ps.ctx.Done():
case <-done:
}
}
// TryPub publishes the given message with Non-Blocking semantics
// Use this only for messages that are ok to lose
// Use this only if your calling function must never ever block.
// returns an error on congestion of cmd-pipe, in which case you may try again,
// since the cmd-pipe is filled with sub-requests and other threads messages
// and can easily become full.
// Note that err=nil does not mean the message arrived, it could still
// get lost at the receivers end if that channel is full
// If err!=nil, one idea could be to spawn an anonymous goroutine to try again
func (ps *pubSub) TryPub(msg interface{}, topics []string, opts ...PubOption) bool {
done := make(chan bool, 1)
op := pubOptions{}
for _, opt := range opts {
opt(&op)
}
select {
case <-ps.ctx.Done():
return false
case ps.msgch <- msgEnvelope{topics: topics, done: done, msg: msg, opts: op}:
default:
return false
}
return true
}
// Unsub unsubscribes the given channel from the specified
// topics. If no topic is specified, it is unsubscribed
// from all topics.
func (ps *pubSub) Unsub(ch <-chan interface{}, topics ...string) {
done := make(chan bool, 1)
c := cmd{
op: unsub,
topics: topics,
ch: ch,
done: done,
}
select {
case <-ps.ctx.Done():
case ps.cmdch <- c:
}
select {
case <-ps.ctx.Done():
case <-done:
}
}
// Shutdown closes all subscribed channels and terminates the goroutine.
func (ps *pubSub) Shutdown() {
select {
case <-ps.ctx.Done():
return
default:
}
ps.cancel()
_ = ps.group.Wait()
}
// Main Cmd Handling Goroutine
func (ps *pubSub) run() error {
for {
select {
case <-ps.ctx.Done():
return ps.ctx.Err()
case cmd := <-ps.cmdch:
switch cmd.op {
case sub:
fallthrough
case subOnce:
subs := &subscriber{
ctx: ps.ctx,
och: cmd.ch.(chan interface{}),
ich: make(chan interface{}, 1),
done: make(chan struct{}),
cmdch: ps.cmdch,
once: cmd.op == subOnce,
}
ps.add(cmd.topics, subs)
ps.group.Go(subs.run)
case subAdd:
ch := cmd.ch.(<-chan interface{})
subs := ps.revTopics[ch].sub
ps.add(cmd.topics, subs)
case unsub:
ps.remove(cmd.topics, cmd.ch.(<-chan interface{}))
}
cmd.done <- true
case envelope := <-ps.msgch:
for _, topic := range envelope.topics {
ps.send(topic, envelope.msg, envelope.opts)
}
envelope.done <- true
}
}
}
func (s *subscriber) run() error {
defer func() {
close(s.och)
}()
var pending []interface{}
var msg interface{}
var och chan interface{}
var cmdch chan<- cmd
done := make(chan bool, 1)
for {
select {
case <-s.ctx.Done():
return s.ctx.Err()
case <-s.done:
return nil
case <-done:
return nil
case rMsg := <-s.ich:
if cmdch == nil {
pending = append(pending, rMsg)
if och == nil {
msg = pending[0]
och = s.och
}
}
case och <- msg:
if s.once {
cmdch = s.cmdch
och = nil
break
}
pending = pending[1:]
if len(pending) > 0 {
msg = pending[0]
} else {
och = nil
}
case cmdch <- cmd{op: unsub, ch: (<-chan interface{})(s.och), done: done}:
}
}
}
func (ps *pubSub) add(topics []string, sub *subscriber) {
for _, tn := range topics {
tp := ps.topics[tn]
if tp == nil {
tp = &topic{}
ps.topics[tn] = tp
}
if tp.subs == nil {
tp.subs = make(topicSubs)
}
tp.subs[sub.och] = sub
if ps.revTopics[sub.och] == nil {
ps.revTopics[sub.och] = &revSubscriber{
sub: sub,
topics: make(map[string]bool),
}
}
ps.revTopics[sub.och].topics[tn] = true
if tp.retained != nil {
sub.ich <- tp.retained
}
}
}
func (ps *pubSub) send(to string, msg interface{}, opts pubOptions) {
tp := ps.topics[to]
if tp == nil && !opts.retain {
return
}
if msg == nil && tp != nil {
tp.retained = nil
if len(ps.topics[to].subs) == 0 {
delete(ps.topics, to)
}
return
}
if tp == nil {
tp = &topic{
subs: make(topicSubs),
}
}
ps.topics[to] = tp
if opts.retain {
tp.retained = msg
}
for _, sub := range tp.subs {
select {
case <-ps.ctx.Done():
return
case sub.ich <- msg:
}
}
}
func (ps *pubSub) remove(topics []string, ch <-chan interface{}) {
subscribedTopics := ps.revTopics[ch].topics
subs := ps.revTopics[ch].sub
if len(topics) == 0 {
topics = make([]string, 0, len(subscribedTopics))
for topic := range subscribedTopics {
topics = append(topics, topic)
}
}
for _, topic := range topics {
delete(ps.topics[topic].subs, ch)
delete(ps.revTopics[ch].topics, topic)
}
if len(ps.revTopics[ch].topics) == 0 {
ps.revTopics[ch].sub = nil
close(subs.done)
delete(ps.revTopics, ch)
}
}