-
Notifications
You must be signed in to change notification settings - Fork 113
/
pubsub_persistent.go
316 lines (276 loc) · 7.75 KB
/
pubsub_persistent.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
package radix
import (
"fmt"
"sync"
"time"
)
type persistentPubSubOpts struct {
connFn ConnFunc
abortAfter int
errCh chan<- error
}
// PersistentPubSubOpt is an optional parameter which can be passed into
// PersistentPubSub in order to affect its behavior.
type PersistentPubSubOpt func(*persistentPubSubOpts)
// PersistentPubSubConnFunc causes PersistentPubSub to use the given ConnFunc
// when connecting to its destination.
func PersistentPubSubConnFunc(connFn ConnFunc) PersistentPubSubOpt {
return func(opts *persistentPubSubOpts) {
opts.connFn = connFn
}
}
// PersistentPubSubAbortAfter changes PersistentPubSub's reconnect behavior.
// Usually PersistentPubSub will try to reconnect forever upon a disconnect,
// blocking any methods which have been called until reconnect is successful.
//
// When PersistentPubSubAbortAfter is used, it will give up after that many
// attempts and return the error to the method which has been blocked the
// longest. Another method will need to be called in order for PersistentPubSub
// to resume trying to reconnect.
func PersistentPubSubAbortAfter(attempts int) PersistentPubSubOpt {
return func(opts *persistentPubSubOpts) {
opts.abortAfter = attempts
}
}
// PersistentPubSubErrCh takes a channel which asynchronous errors
// encountered by the PersistentPubSub can be read off of. If the channel blocks
// the error will be dropped. The channel will be closed when PersistentPubSub
// is closed.
func PersistentPubSubErrCh(errCh chan<- error) PersistentPubSubOpt {
return func(opts *persistentPubSubOpts) {
opts.errCh = errCh
}
}
type pubSubCmd struct {
// msgCh can be set along with one of subscribe/unsubscribe/etc...
msgCh chan<- PubSubMessage
subscribe, unsubscribe, psubscribe, punsubscribe []string
// ... or one of ping or close can be set
ping, close bool
// resCh is always set
resCh chan error
}
type persistentPubSub struct {
dial func() (Conn, error)
opts persistentPubSubOpts
subs, psubs chanSet
curr PubSubConn
currErrCh chan error
cmdCh chan pubSubCmd
closeErr error
closeCh chan struct{}
closeOnce sync.Once
}
// PersistentPubSubWithOpts is like PubSub, but instead of taking in an existing
// Conn to wrap it will create one on the fly. If the connection is ever
// terminated then a new one will be created and will be reset to the previous
// connection's state.
//
// This is effectively a way to have a permanent PubSubConn established which
// supports subscribing/unsubscribing but without the hassle of implementing
// reconnect/re-subscribe logic.
//
// With default options, neither this function nor any of the methods on the
// returned PubSubConn will ever return an error, they will instead block until
// a connection can be successfully reinstated.
//
// PersistentPubSubWithOpts takes in a number of options which can overwrite its
// default behavior. The default options PersistentPubSubWithOpts uses are:
//
// PersistentPubSubConnFunc(DefaultConnFunc)
//
func PersistentPubSubWithOpts(
network, addr string, options ...PersistentPubSubOpt,
) (
PubSubConn, error,
) {
opts := persistentPubSubOpts{
connFn: DefaultConnFunc,
}
for _, opt := range options {
opt(&opts)
}
p := &persistentPubSub{
dial: func() (Conn, error) { return opts.connFn(network, addr) },
opts: opts,
subs: chanSet{},
psubs: chanSet{},
cmdCh: make(chan pubSubCmd),
closeCh: make(chan struct{}),
}
if err := p.refresh(); err != nil {
return nil, err
}
go p.spin()
return p, nil
}
// PersistentPubSub is deprecated in favor of PersistentPubSubWithOpts instead.
func PersistentPubSub(network, addr string, connFn ConnFunc) PubSubConn {
var opts []PersistentPubSubOpt
if connFn != nil {
opts = append(opts, PersistentPubSubConnFunc(connFn))
}
// since PersistentPubSubAbortAfter isn't used, this will never return an
// error, panic if it does
p, err := PersistentPubSubWithOpts(network, addr, opts...)
if err != nil {
panic(fmt.Sprintf("PersistentPubSubWithOpts impossibly returned an error: %v", err))
}
return p
}
// refresh only returns an error if the connection could not be made.
func (p *persistentPubSub) refresh() error {
if p.curr != nil {
p.curr.Close()
<-p.currErrCh
p.curr = nil
p.currErrCh = nil
}
attempt := func() (PubSubConn, chan error, error) {
c, err := p.dial()
if err != nil {
return nil, nil, err
}
errCh := make(chan error, 1)
pc := newPubSub(c, errCh)
for msgCh, channels := range p.subs.inverse() {
if err := pc.Subscribe(msgCh, channels...); err != nil {
pc.Close()
return nil, nil, err
}
}
for msgCh, patterns := range p.psubs.inverse() {
if err := pc.PSubscribe(msgCh, patterns...); err != nil {
pc.Close()
return nil, nil, err
}
}
return pc, errCh, nil
}
var attempts int
for {
var err error
if p.curr, p.currErrCh, err = attempt(); err == nil {
return nil
}
attempts++
if p.opts.abortAfter > 0 && attempts >= p.opts.abortAfter {
return err
}
time.Sleep(200 * time.Millisecond)
}
}
func (p *persistentPubSub) execCmd(cmd pubSubCmd) error {
if p.curr == nil {
if err := p.refresh(); err != nil {
return err
}
}
// For all subscribe/unsubscribe/etc... commands the modifications to
// p.subs/p.psubs are made first, so that if the actual call to curr fails
// then refresh will still instate the new desired subscription.
var err error
switch {
case len(cmd.subscribe) > 0:
for _, channel := range cmd.subscribe {
p.subs.add(channel, cmd.msgCh)
}
err = p.curr.Subscribe(cmd.msgCh, cmd.subscribe...)
case len(cmd.unsubscribe) > 0:
for _, channel := range cmd.unsubscribe {
p.subs.del(channel, cmd.msgCh)
}
err = p.curr.Unsubscribe(cmd.msgCh, cmd.unsubscribe...)
case len(cmd.psubscribe) > 0:
for _, channel := range cmd.psubscribe {
p.psubs.add(channel, cmd.msgCh)
}
err = p.curr.PSubscribe(cmd.msgCh, cmd.psubscribe...)
case len(cmd.punsubscribe) > 0:
for _, channel := range cmd.punsubscribe {
p.psubs.del(channel, cmd.msgCh)
}
err = p.curr.PUnsubscribe(cmd.msgCh, cmd.punsubscribe...)
case cmd.ping:
err = p.curr.Ping()
case cmd.close:
if p.curr != nil {
err = p.curr.Close()
<-p.currErrCh
}
default:
// don't do anything I guess
}
if err != nil {
return p.refresh()
}
return nil
}
func (p *persistentPubSub) err(err error) {
select {
case p.opts.errCh <- err:
default:
}
}
func (p *persistentPubSub) spin() {
for {
select {
case err := <-p.currErrCh:
p.err(err)
if err := p.refresh(); err != nil {
p.err(err)
}
case cmd := <-p.cmdCh:
cmd.resCh <- p.execCmd(cmd)
if cmd.close {
return
}
}
}
}
func (p *persistentPubSub) cmd(cmd pubSubCmd) error {
cmd.resCh = make(chan error, 1)
select {
case p.cmdCh <- cmd:
return <-cmd.resCh
case <-p.closeCh:
return fmt.Errorf("closed")
}
}
func (p *persistentPubSub) Subscribe(msgCh chan<- PubSubMessage, channels ...string) error {
return p.cmd(pubSubCmd{
msgCh: msgCh,
subscribe: channels,
})
}
func (p *persistentPubSub) Unsubscribe(msgCh chan<- PubSubMessage, channels ...string) error {
return p.cmd(pubSubCmd{
msgCh: msgCh,
unsubscribe: channels,
})
}
func (p *persistentPubSub) PSubscribe(msgCh chan<- PubSubMessage, channels ...string) error {
return p.cmd(pubSubCmd{
msgCh: msgCh,
psubscribe: channels,
})
}
func (p *persistentPubSub) PUnsubscribe(msgCh chan<- PubSubMessage, channels ...string) error {
return p.cmd(pubSubCmd{
msgCh: msgCh,
punsubscribe: channels,
})
}
func (p *persistentPubSub) Ping() error {
return p.cmd(pubSubCmd{ping: true})
}
func (p *persistentPubSub) Close() error {
p.closeOnce.Do(func() {
p.closeErr = p.cmd(pubSubCmd{close: true})
close(p.closeCh)
if p.opts.errCh != nil {
close(p.opts.errCh)
}
})
return p.closeErr
}