-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
355 lines (315 loc) · 8.91 KB
/
client.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
// The pubsub package provides a useful stable Redis pubsub connection.
// After opening a connection, it allows you to subscribe to and receieve
// events even in the case of network failures - you don't have to deal
// with that in your code!
//
// Basic usage, prints any messages it gets in the "foobar" channel:
//
// client := NewPubsub("127.0.0.1:6379")
// defer client.TearDown()
// go client.Connect()
//
// listener := client.Listener(Channel, "foobar")
// for {
// fmt.Println(<-listener.Channel)
// }
//
// Events are emitted down the client's "Event" channel. If you wanted to
// wait until the client was open (not necessary, but may be useful):
//
// client := NewPubsub("127.0.0.1:6379")
// go client.Connect()
// client.WaitFor(ConnectedEvent)
//
// You can also subscribe to patterns and unsubscribe, of course:
//
// listener := client.Listener(Pattern, "foo:*:bar")
// doStuff()
// listener.Unsubscribe()
package pubsub
import (
"net"
"sync"
"time"
"github.com/WatchBeam/fsm"
"github.com/garyburd/redigo/redis"
)
// Used to denote the parameters of the redis connection.
type ConnectionParam struct {
// Host:port
Address string
// Optional password. Defaults to no authentication.
Password string
// Policy to use for reconnections (defaults to
// LogReconnectPolicy with a base of 10 and factor of 1 ms)
Policy ReconnectPolicy
// Dial timeout for redis (defaults to no timeout)
Timeout time.Duration
}
// Used to denote the type of listener - channel or pattern.
type ListenerType uint8
const (
Channel ListenerType = iota
Pattern
)
// Tasks we send to the main pubsub thread to subscribe/unsubscribe.
type task struct {
Action action
Listener *Listener
// If true, the *action* (subscribed/unsubscribed) will be undertaken
// even if we think we might have already done it.
Force bool
}
// actions are "things" we can do in tasks
type action uint8
// List of actions we can use in tasks. Internal use
const (
subscribeAction action = iota
unsubscribeAction
disruptAction
)
// The Client is responsible for maintaining a subscribed redis client,
// reconnecting and resubscribing if it drops.
type Client struct {
eventEmitter
// The current state that the client is in.
state *fsm.Machine
stateLock *sync.Mutex
// Connection params we're subbed to.
conn ConnectionParam
// The subscription client we're currently using.
pubsub *redis.PubSubConn
// A list of events that we're subscribed to. If the connection closes,
// we'll reestablish it and resubscribe to events.
subscribed map[string][]*Listener
// Reconnection policy.
policy ReconnectPolicy
// Channel of sub/unsub tasks
tasks chan task
}
// Creates and returns a new pubsub client client and subscribes to it.
func New(conn ConnectionParam) *Client {
client := &Client{
eventEmitter: newEventEmitter(),
state: blueprint.Machine(),
stateLock: new(sync.Mutex),
conn: conn,
subscribed: map[string][]*Listener{},
tasks: make(chan task, 128),
}
if conn.Policy != nil {
client.policy = conn.Policy
} else {
client.policy = &LogReconnectPolicy{Base: 10, Factor: time.Millisecond}
}
return client
}
// Convenience function to create a new listener for an event.
func (c *Client) Listener(kind ListenerType, event string) *Listener {
listener := &Listener{
Type: kind,
Event: event,
Messages: make(chan redis.Message),
PMessages: make(chan redis.PMessage),
Client: c,
}
c.Subscribe(listener)
return listener
}
// Gets the current client state.
func (c *Client) GetState() uint8 {
c.stateLock.Lock()
defer c.stateLock.Unlock()
return c.state.State()
}
// Sets the client state in a thread-safe manner.
func (c *Client) setState(s uint8) error {
c.stateLock.Lock()
err := c.state.Goto(s)
c.stateLock.Unlock()
if err != nil {
return err
}
switch s {
case ConnectedState:
c.emit(ConnectedEvent, nil)
case DisconnectedState:
c.emit(DisconnectedEvent, nil)
case ClosingState:
c.emit(ClosingEvent, nil)
case ClosedState:
c.emit(ClosedEvent, nil)
}
return nil
}
// Tries to reconnect to pubsub, looping until we're able to do so
// successfully. This must be called to activate the client.
func (c *Client) Connect() {
for c.GetState() == DisconnectedState {
go c.resubscribe()
c.doConnection()
time.Sleep(c.policy.Next())
}
c.setState(ClosedState)
}
func (c *Client) doConnection() {
var cnx redis.Conn
var err error
if c.conn.Timeout > 0 {
cnx, err = redis.DialTimeout("tcp", c.conn.Address,
c.conn.Timeout, c.conn.Timeout, c.conn.Timeout)
} else {
cnx, err = redis.Dial("tcp", c.conn.Address)
}
if err != nil {
c.emit(ErrorEvent, err)
return
}
if c.conn.Password != "" {
if _, err := cnx.Do("AUTH", c.conn.Password); err != nil {
c.emit(ErrorEvent, err)
c.setState(ClosingState)
}
}
c.pubsub = &redis.PubSubConn{Conn: cnx}
c.policy.Reset()
c.setState(ConnectedState)
end := make(chan bool)
go func() {
for {
select {
case <-end:
return
case t := <-c.tasks:
c.workOnTask(t)
}
}
}()
READ:
for c.GetState() == ConnectedState {
switch reply := c.pubsub.Receive().(type) {
case redis.Message:
go c.dispatchMessage(reply)
case redis.PMessage:
go c.dispatchPMessage(reply)
case redis.Subscription:
switch reply.Kind {
case "subscribe", "psubscribe":
c.emit(SubscribeEvent, reply)
case "unsubscribe", "punsubscribe":
c.emit(UnsubscribeEvent, reply)
}
case error:
if err, ok := reply.(net.Error); ok && err.Timeout() {
// don't emit error for time outs
} else if c.GetState() != ConnectedState {
// if we already closed, don't really care
} else {
c.emit(ErrorEvent, reply)
}
break READ
}
}
end <- true
c.pubsub.Close()
c.setState(DisconnectedState)
}
// Takes a task, modifies redis and the internal subscribed registery.
// This is done here (called in boot()) since Go's maps are not thread safe.
func (c *Client) workOnTask(t task) {
switch t.Action {
case subscribeAction:
event := t.Listener.Event
// Check to see if it already exists in the subscribed map.
// If not, then we need to create a new listener list and
// start listening on the pubsub client. Otherwise just
// append it.
if _, exists := c.subscribed[event]; !exists || t.Force {
c.subscribed[event] = []*Listener{t.Listener}
switch t.Listener.Type {
case Channel:
c.pubsub.Subscribe(event)
case Pattern:
c.pubsub.PSubscribe(event)
}
} else {
c.subscribed[event] = append(c.subscribed[event], t.Listener)
}
case unsubscribeAction:
event := t.Listener.Event
// Look for the listener in the subscribed list and,
// once found, remove it.
list := c.subscribed[event]
for i, l := range list {
if l == t.Listener {
c.subscribed[event] = append(list[:i], list[i+1:]...)
break
}
}
// If the list is now empty, we can unsubscribe on Redis and
// remove it from our registery
if len(c.subscribed[event]) == 0 || t.Force {
switch t.Listener.Type {
case Channel:
c.pubsub.Unsubscribe(event)
case Pattern:
c.pubsub.PUnsubscribe(event)
}
delete(c.subscribed, event)
}
case disruptAction:
c.pubsub.Conn.Close()
default:
panic("unknown task")
}
}
// Takes in a Redis message and sends it out to any "listening" channels.
func (c *Client) dispatchMessage(message redis.Message) {
if listeners, exists := c.subscribed[message.Channel]; exists {
for _, l := range listeners {
l.Messages <- message
}
}
}
// Takes in a Redis pmessage and sends it out to any "listening" channels.
func (c *Client) dispatchPMessage(message redis.PMessage) {
if listeners, exists := c.subscribed[message.Pattern]; exists {
for _, l := range listeners {
l.PMessages <- message
}
}
}
// Resubscribes to all Redis events. Good to do after a disconnection.
func (c *Client) resubscribe() {
// Swap so that if we reconnect before all tasks are done,
// we don't duplicate things.
subs := c.subscribed
c.subscribed = map[string][]*Listener{}
for _, events := range subs {
// We only need to subscribe on one event, so that the Redis
// connection gets registered. We don't care about the others.
c.tasks <- task{Listener: events[0], Action: subscribeAction, Force: true}
}
}
// Tears down the client - closes the connection and stops
// listening for connections.
func (c *Client) TearDown() {
if c.GetState() != ConnectedState {
return
}
c.setState(ClosingState)
c.tasks <- task{Action: disruptAction}
c.WaitFor(ClosedEvent)
}
// Subscribes to a Redis event. Strings are sent back down the listener
// channel when they come in, and
func (c *Client) Subscribe(listener *Listener) {
listener.Active = true
c.tasks <- task{Listener: listener, Action: subscribeAction}
}
// Removes the listener from the list of subscribers. If it's the last one
// listening to that Redis event, we unsubscribe entirely.
func (c *Client) Unsubscribe(listener *Listener) {
listener.Active = false
c.tasks <- task{Listener: listener, Action: unsubscribeAction}
}