-
Notifications
You must be signed in to change notification settings - Fork 65
/
pubsub.go
264 lines (219 loc) · 6.06 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
// 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 LICENSE file.
// Package pubsub implements a simple multi-topic pub-sub
// library.
//
// A topic can have any number of subscribers. All subscribers receive messages
// published on the topic.
package pubsub
type operation int
const (
sub operation = iota
subOnce
subOnceEach
pub
tryPub
unsub
unsubAll
closeTopic
shutdown
)
// PubSub is a collection of topics.
type PubSub[T comparable, M any] struct {
cmdChan chan cmd[T, M]
capacity int
}
type cmd[T comparable, M any] struct {
msg M
ch chan M
topics []T
op operation
}
// New creates a new PubSub and starts a goroutine for handling operations. Sub
// and SubOnce will create channels with the given capacity.
func New[T comparable, M any](capacity int) *PubSub[T, M] {
ps := &PubSub[T, M]{make(chan cmd[T, M]), capacity}
go ps.start()
return ps
}
// Sub returns a channel from which messages published on the specified topics
// can be received.
func (ps *PubSub[T, M]) Sub(topics ...T) chan M {
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.
func (ps *PubSub[T, M]) SubOnce(topics ...T) chan M {
return ps.sub(subOnce, topics...)
}
// SubOnceEach returns a channel on which callers receive, at most, one message
// for each topic.
func (ps *PubSub[T, M]) SubOnceEach(topics ...T) chan M {
return ps.sub(subOnceEach, topics...)
}
func (ps *PubSub[T, M]) sub(op operation, topics ...T) chan M {
ch := make(chan M, ps.capacity)
ps.cmdChan <- cmd[T, M]{op: op, topics: topics, ch: ch}
return ch
}
// AddSub adds subscriptions to an existing channel.
func (ps *PubSub[T, M]) AddSub(ch chan M, topics ...T) {
ps.cmdChan <- cmd[T, M]{op: sub, topics: topics, ch: ch}
}
// AddSubOnceEach adds subscriptions to an existing channel with SubOnceEach
// behavior.
func (ps *PubSub[T, M]) AddSubOnceEach(ch chan M, topics ...T) {
ps.cmdChan <- cmd[T, M]{op: subOnceEach, topics: topics, ch: ch}
}
// Pub publishes the given message to all subscribers of the specified topics.
func (ps *PubSub[T, M]) Pub(msg M, topics ...T) {
ps.cmdChan <- cmd[T, M]{op: pub, topics: topics, msg: msg}
}
// TryPub publishes the given message to all subscribers of the specified topics
// if the topic has buffer space.
func (ps *PubSub[T, M]) TryPub(msg M, topics ...T) {
ps.cmdChan <- cmd[T, M]{op: tryPub, topics: topics, msg: msg}
}
// Unsub unsubscribes the given channel from the specified topics. If no topic
// is specified, it is unsubscribed from all topics.
//
// Unsub must be called from a goroutine that is different from the subscriber.
// The subscriber must consume messages from the channel until it reaches the
// end. Not doing so can result in a deadlock.
func (ps *PubSub[T, M]) Unsub(ch chan M, topics ...T) {
if len(topics) == 0 {
ps.cmdChan <- cmd[T, M]{op: unsubAll, ch: ch}
return
}
ps.cmdChan <- cmd[T, M]{op: unsub, topics: topics, ch: ch}
}
// Close closes all channels currently subscribed to the specified topics. If a
// channel is subscribed to multiple topics, some of which is not specified, it
// is not closed.
func (ps *PubSub[T, M]) Close(topics ...T) {
ps.cmdChan <- cmd[T, M]{op: closeTopic, topics: topics}
}
// Shutdown closes all subscribed channels and terminates the goroutine.
func (ps *PubSub[T, M]) Shutdown() {
ps.cmdChan <- cmd[T, M]{op: shutdown}
}
func (ps *PubSub[T, M]) start() {
reg := registry[T, M]{
topics: make(map[T]map[chan M]subType),
revTopics: make(map[chan M]map[T]bool),
}
loop:
for cmd := range ps.cmdChan {
if cmd.topics == nil {
switch cmd.op {
case unsubAll:
reg.removeChannel(cmd.ch)
case shutdown:
break loop
}
continue loop
}
for _, topic := range cmd.topics {
switch cmd.op {
case sub:
reg.add(topic, cmd.ch, normal)
case subOnce:
reg.add(topic, cmd.ch, onceAny)
case subOnceEach:
reg.add(topic, cmd.ch, onceEach)
case tryPub:
reg.sendNoWait(topic, cmd.msg)
case pub:
reg.send(topic, cmd.msg)
case unsub:
reg.remove(topic, cmd.ch)
case closeTopic:
reg.removeTopic(topic)
}
}
}
for topic, chans := range reg.topics {
for ch := range chans {
reg.remove(topic, ch)
}
}
}
// registry maintains the current subscription state. It is not safe to access a
// registry from multiple goroutines concurrently.
type registry[T comparable, M any] struct {
topics map[T]map[chan M]subType
revTopics map[chan M]map[T]bool
}
type subType int
const (
onceAny subType = iota
onceEach
normal
)
func (reg *registry[T, M]) add(topic T, ch chan M, st subType) {
if reg.topics[topic] == nil {
reg.topics[topic] = make(map[chan M]subType)
}
reg.topics[topic][ch] = st
if reg.revTopics[ch] == nil {
reg.revTopics[ch] = make(map[T]bool)
}
reg.revTopics[ch][topic] = true
}
func (reg *registry[T, M]) send(topic T, msg M) {
for ch, st := range reg.topics[topic] {
ch <- msg
switch st {
case onceAny:
for topic := range reg.revTopics[ch] {
reg.remove(topic, ch)
}
case onceEach:
reg.remove(topic, ch)
}
}
}
func (reg *registry[T, M]) sendNoWait(topic T, msg M) {
for ch, st := range reg.topics[topic] {
select {
case ch <- msg:
switch st {
case onceAny:
for topic := range reg.revTopics[ch] {
reg.remove(topic, ch)
}
case onceEach:
reg.remove(topic, ch)
}
default:
}
}
}
func (reg *registry[T, M]) removeTopic(topic T) {
for ch := range reg.topics[topic] {
reg.remove(topic, ch)
}
}
func (reg *registry[T, M]) removeChannel(ch chan M) {
for topic := range reg.revTopics[ch] {
reg.remove(topic, ch)
}
}
func (reg *registry[T, M]) remove(topic T, ch chan M) {
if _, ok := reg.topics[topic]; !ok {
return
}
if _, ok := reg.topics[topic][ch]; !ok {
return
}
delete(reg.topics[topic], ch)
delete(reg.revTopics[ch], topic)
if len(reg.topics[topic]) == 0 {
delete(reg.topics, topic)
}
if len(reg.revTopics[ch]) == 0 {
close(ch)
delete(reg.revTopics, ch)
}
}