-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
342 lines (320 loc) · 8.22 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
package greddis
import (
"context"
"errors"
"fmt"
"log"
"math"
"net"
"sync"
"time"
"unsafe"
)
// TODO do multi-core benchmarks to ensure it scales ok
const (
subTopic = iota + 1
subPattern
)
// I can't really have more than one message per connection "active" at a time
// because the reader, the result etc are all tied to the connection (and we should really not
// be advancing the reader in another thread) - so the idea here with the pool and stuff
// is pretty stupid. I should add the message to the subscription manager and listen for it
// on the channel at the top of every loop of Listen()
type Message struct {
Ctx context.Context
pattern []byte
Result *Result
topic []byte
}
func (m *Message) Init() {
m.pattern = m.pattern[:0]
m.topic = m.topic[:0]
}
func (m *Message) Pattern() string {
return *(*string)(unsafe.Pointer(&m.pattern))
}
func (m *Message) Topic() string {
return *(*string)(unsafe.Pointer(&m.topic))
}
func newMessage(res *Result, msgChan chan *Message) *Message {
msg := &Message{
Result: res,
pattern: make([]byte, 0, 100),
topic: make([]byte, 0, 100),
}
res.finish = func() {
msgChan <- msg
}
return msg
}
type MessageChan <-chan *Message
type MessageChanMap map[string]MessageChan
type subscription struct {
topic string
msgChan chan *Message
subType int
}
func newSubscription(topic interface{}, bufLen int) *subscription {
var t string
var s int
var msgChan chan *Message
if bufLen > 0 {
msgChan = make(chan *Message, bufLen)
} else {
msgChan = make(chan *Message)
}
switch d := topic.(type) {
case string:
t = d
s = subTopic
case RedisPattern:
t = string(d)
s = subPattern
}
return &subscription{
topic: t,
msgChan: msgChan,
subType: s,
}
}
type subscriptionManager struct {
listening bool
chans *sync.Map
conn *conn
connPool internalPool
msgChan chan *Message
opts *PubSubOpts
writeLock *sync.Mutex
readChan chan string
}
func newSubscriptionManager(pool internalPool, opts *PubSubOpts) *subscriptionManager {
// NewResult is called explicitly after a connection is established in the general case.
// However subscriptionManager manages a connection set in subscriber mode, so when we instantiate
// a subscriptionManager we do not have a connection yet and thus we pass in nil to NewResult
// and swap it out later
mngr := &subscriptionManager{
opts: opts,
chans: &sync.Map{},
connPool: pool,
msgChan: make(chan *Message),
writeLock: &sync.Mutex{},
readChan: make(chan string),
listening: false,
}
return mngr
}
func (s *subscriptionManager) getConn(ctx context.Context) (*conn, error) {
var err error
if s.conn == nil {
s.conn, err = s.connPool.Get(ctx)
}
return s.conn, err
}
func (s *subscriptionManager) Listen(ctx context.Context, c *conn) {
var count int
var errs []error
var err error
array := NewArrayReader(c.r)
for {
select {
case <-ctx.Done():
log.Printf("%s\n", ctx.Err())
return
case m := <-s.msgChan:
if m == nil {
return
}
m.Init()
c.conn.SetReadDeadline(time.Now().Add(s.opts.PingInterval))
err = array.Init(ScanBulkString)
// TODO when 1.15 is out, change this to this: https://github.com/golang/go/issues/31449
if nErr, ok := err.(net.Error); ok && nErr.Timeout() {
c.conn.SetReadDeadline(time.Now().Add(s.opts.ReadTimeout))
if err := ping(ctx, c); err != nil {
log.Printf("Ping error: %s", err)
return
}
continue
} else if err != nil {
log.Printf("Array Init error: %s", err)
return
} else {
c.conn.SetReadDeadline(time.Now().Add(s.opts.ReadTimeout))
}
switch array.Next().SwitchOnNext() {
case "pmessage":
errs = append(errs, array.Next().Scan(&m.pattern))
fallthrough
case "message":
errs = append(errs, array.Next().Scan(&m.topic))
case "psubscribe":
fallthrough
case "subscribe":
fallthrough
case "punsubscribe":
fallthrough
case "unsubscribe":
for i := 0; i < int(math.Ceil(float64(array.Len())/2)-1); i++ {
errs = append(errs, array.Next().Expect(<-s.readChan))
errs = append(errs, array.NextIs(ScanInteger).Scan(&count))
}
default:
v, _ := array.r.r.Peek(array.r.r.Buffered())
log.Printf(
"We shouldn't be here :( value: '%s' buffered: %d\nContents: %s",
array.r.String(),
array.r.r.Buffered(),
v,
)
}
encounteredErr := false
for _, err := range errs {
if err != nil {
encounteredErr = true
log.Printf("Error: %s", err)
}
}
errs = errs[:0]
if encounteredErr {
// TODO we should be trying to close the connection sanely here?
return
}
if array.Len() > array.pos {
array.Next()
if array.Err() != nil {
log.Printf("Next error: %s\n", array.Err())
}
s.dispatch(ctx, m)
} else {
go func() { s.msgChan <- m }()
}
}
}
}
// Design publish command -> publish to channel that we're expecting a read, wait for return on second chan
func (s *subscriptionManager) Subscribe(ctx context.Context, topics ...interface{}) (MessageChanMap, error) {
if len(topics) == 0 {
return nil, errors.New("No topics given to subscribe to")
}
if !s.listening {
conn, err := s.getConn(ctx)
if err != nil {
return nil, err
}
go s.Listen(ctx, conn)
s.msgChan <- newMessage(NewResult(conn.r), s.msgChan)
s.listening = true
}
subs := make([]*subscription, len(topics))
chanMap := make(MessageChanMap, len(topics))
s.writeLock.Lock()
arrw := s.conn.arrw
var subType int
switch topics[0].(type) {
case RedisPattern:
subType = subPattern
arrw.Init(len(topics) + 1).AddString("PSUBSCRIBE")
case string:
subType = subTopic
arrw.Init(len(topics) + 1).AddString("SUBSCRIBE")
}
for i, topic := range topics {
var sub *subscription
switch d := topic.(type) {
case RedisPattern:
if subType != subPattern {
s.writeLock.Unlock()
return nil, ErrMixedTopicTypes
}
case string:
if subType != subTopic {
s.writeLock.Unlock()
return nil, ErrMixedTopicTypes
}
default:
s.writeLock.Unlock()
return nil, fmt.Errorf("Wrong type! Expected RedisPattern or string, but received: %T", d)
}
sub = newSubscription(topic, s.opts.InitBufSize)
arrw.AddString(sub.topic)
subs[i] = sub
chanMap[sub.topic] = sub.msgChan
}
var err error
err = arrw.Flush()
s.writeLock.Unlock()
if err != nil {
return nil, err
}
// Need to store the subscriptions after we flush the command so we only store valid subscriptions
for _, sub := range subs {
s.chans.Store(sub.topic, sub)
s.readChan <- sub.topic
}
// TODO If subscriptionCount drops to 0, we can hand back the connection and kill our poor listener
// this depends on the configuration of the subscriber portion
return chanMap, nil
}
func (s *subscriptionManager) dispatch(ctx context.Context, m *Message) {
m.Ctx = ctx
key := m.Topic()
if m.Pattern() != "" {
key = m.Pattern()
}
if sub, ok := s.chans.Load(key); ok {
select {
case <-ctx.Done():
return
case sub.(*subscription).msgChan <- m:
}
}
}
func (s *subscriptionManager) Unsubscribe(ctx context.Context, topics ...interface{}) error {
// TODO support a toggle to kill the loop so that when subscription count reaches 0, it exits cleanly
conn, err := s.getConn(ctx)
if err != nil {
return err
}
arrw := conn.arrw
var subType int
switch topics[0].(type) {
case RedisPattern:
subType = subPattern
arrw.Init(len(topics) + 1).AddString("PUNSUBSCRIBE")
case string:
subType = subTopic
arrw.Init(len(topics) + 1).AddString("UNSUBSCRIBE")
}
subs := make([]*subscription, 0, len(topics))
for _, topic := range topics {
switch d := topic.(type) {
case RedisPattern:
if subType != 0 && subType != subPattern {
return ErrMixedTopicTypes
}
arrw.AddString(string(d))
if sub, ok := s.chans.Load(string(d)); ok {
subs = append(subs, sub.(*subscription))
}
case string:
if subType != 0 && subType != subTopic {
return ErrMixedTopicTypes
}
arrw.AddString(d)
if sub, ok := s.chans.Load(d); ok {
subs = append(subs, sub.(*subscription))
}
default:
return fmt.Errorf("Wrong type! Expected RedisPattern or string, but received: %T", d)
}
}
err = arrw.Flush()
if err != nil {
return err
}
for _, sub := range subs {
s.chans.Delete(sub.topic)
s.readChan <- sub.topic
}
return nil
}