forked from autogrow/wray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwray.go
528 lines (436 loc) · 13 KB
/
wray.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package wray
import (
"errors"
"fmt"
"log"
"path/filepath"
"strings"
"sync"
"time"
)
const (
UNCONNECTED = 1
CONNECTING = 2
CONNECTED = 3
DISCONNECTED = 4
HANDSHAKE = "handshake"
RETRY = "retry"
NONE = "none"
CONNECTION_TIMEOUT = 60.0
DEFAULT_RETRY = 5.0
MAX_REQUEST_SIZE = 2048
)
var (
MANDATORY_CONNECTION_TYPES = []string{"long-polling"}
registeredTransports = []Transport{}
)
// Extension models a faye extension
type Extension interface {
In(Message)
Out(Message)
}
// Subscription models a subscription, containing the channel it is subscribed
// to and the chan object used to push messages through
type Subscription struct {
channel string
msgChan chan Message
}
// Logger is the interface that faye uses for it's logger
type Logger interface {
Infof(f string, a ...interface{})
Errorf(f string, a ...interface{})
Debugf(f string, a ...interface{})
Warnf(f string, a ...interface{})
}
type fayeLogger struct{}
func (l fayeLogger) Infof(f string, a ...interface{}) {
log.Printf("[INFO] : "+f, a...)
}
func (l fayeLogger) Errorf(f string, a ...interface{}) {
log.Printf("[ERROR] : "+f, a...)
}
func (l fayeLogger) Debugf(f string, a ...interface{}) {
log.Printf("[DEBUG] : "+f, a...)
}
func (l fayeLogger) Warnf(f string, a ...interface{}) {
log.Printf("[WARN] : "+f, a...)
}
// FayeClient models a faye client
type FayeClient struct {
state int
url string
subscriptions []*Subscription
transport Transport
log Logger
clientID string
schedular schedular
nextRetry int64
nextHandshake int64
mutex *sync.RWMutex // protects instance vars across goroutines
connectMutex *sync.RWMutex // ensures a single connection to the server as per the protocol
handshakeMutex *sync.RWMutex // ensures only a single handshake at a time
extns []Extension
}
// NewFayeClient returns a new client for interfacing to a faye server
func NewFayeClient(url string) *FayeClient {
return &FayeClient{
url: url,
state: UNCONNECTED,
schedular: channelSchedular{},
mutex: &sync.RWMutex{},
connectMutex: &sync.RWMutex{},
handshakeMutex: &sync.RWMutex{},
log: fayeLogger{},
}
}
// Out spies on outgoing messages and prints them to the log, when the client
// is added to itself as an extension
func (faye *FayeClient) Out(msg Message) {
switch v := msg.(type) {
case msgWrapper:
b, _ := v.MarshalJSON()
faye.log.Debugf("[SPY-OUT] %s\n", string(b))
}
}
// In spies on outgoing messages and prints them to the log, when the client
// is added to itself as an extension
func (faye *FayeClient) In(msg Message) {
switch v := msg.(type) {
case msgWrapper:
b, _ := v.MarshalJSON()
faye.log.Debugf("[SPY-IN] %s\n", string(b))
}
}
// SetLogger attaches a Logger to the faye client, and replaces the default
// logger which just puts to stdout
func (faye *FayeClient) SetLogger(log Logger) {
faye.log = log
}
func (faye *FayeClient) connectIfNotConnected() error {
faye.whileConnectingBlockUntilConnected()
if faye.state == UNCONNECTED {
if err := faye.handshake(); err != nil {
return err
}
}
return nil
}
func (faye *FayeClient) whileConnectingBlockUntilConnected() {
if faye.state == CONNECTING {
for {
if faye.state == CONNECTED {
break
}
time.Sleep(20 * time.Millisecond)
}
}
}
func (faye *FayeClient) handshake() error {
faye.handshakeMutex.Lock()
defer faye.handshakeMutex.Unlock()
// uh oh spaghettios!
if faye.state == DISCONNECTED {
return fmt.Errorf("GTFO: Server told us not to reconnect :(")
}
// check if we need to wait before handshaking again
if faye.nextHandshake > time.Now().Unix() {
sleepFor := time.Now().Unix() - faye.nextHandshake
// wait for the duration the server told us
if sleepFor > 0 {
faye.log.Debugf("Waiting for", sleepFor, "seconds before next handshake")
time.Sleep(time.Duration(sleepFor) * time.Second)
}
}
faye.log.Debugf("Handshaking....")
t, err := selectTransport(faye, MANDATORY_CONNECTION_TYPES, []string{})
if err != nil {
return fmt.Errorf("No usable transports available")
}
faye.mutex.Lock()
faye.transport = t
faye.transport.setURL(faye.url)
faye.state = CONNECTING
faye.mutex.Unlock()
var response Response
for {
msg := faye.newMessage("/meta/handshake")
msg.Version = "1.0"
msg.SupportedConnectionTypes = []string{"long-polling"}
response, _, err = faye.send(msg)
if err != nil {
faye.mutex.Lock()
faye.state = UNCONNECTED
faye.mutex.Unlock()
faye.log.Warnf("Handshake failed. Retry in 10 seconds")
time.Sleep(10 * time.Second)
continue
}
break
}
faye.mutex.Lock()
oldClientID := faye.clientID
faye.clientID = response.ClientID()
faye.state = CONNECTED
faye.transport, err = selectTransport(faye, response.SupportedConnectionTypes(), []string{})
faye.mutex.Unlock()
if err != nil {
return fmt.Errorf("Server does not support any available transports. Supported transports: " + strings.Join(response.SupportedConnectionTypes(), ","))
}
if oldClientID != faye.clientID && len(faye.subscriptions) > 0 {
faye.log.Warnf("Client ID changed (%s => %s), %d invalid subscriptions\n", oldClientID, faye.clientID, len(faye.subscriptions))
faye.resubscribeAll()
}
return nil
}
// change the state in a thread safe manner
func (faye *FayeClient) changeState(state int) {
faye.mutex.Lock()
defer faye.mutex.Unlock()
faye.state = state
}
// TODO: check the bayeux spec to see if the retry period counts for all requests
// func (faye *FayeClient) makeRequest(data map[string]interface{}) (Response, error) {
// faye.connectMutex.Lock()
// defer faye.connectMutex.Unlock()
//
// // wait to retry if we were told to
// if faye.nextRetry > time.Now().Unix() {
// sleepFor := faye.nextRetry - time.Now().Unix()
// if sleepFor > 0 {
// // fmt.Println("Waiting for", sleepFor, "seconds before connecting")
// time.Sleep(time.Duration(sleepFor) * time.Second)
// }
// }
//
// return faye.transport.send(subscriptionParams)
// }
// resubscribe all of the subscriptions
func (faye *FayeClient) resubscribeAll() {
faye.mutex.Lock()
subs := faye.subscriptions
faye.subscriptions = []*Subscription{}
faye.mutex.Unlock()
faye.log.Debugf("Attempting to resubscribe %d subscriptions\n", len(subs))
for _, sub := range subs {
// fork off all the resubscribe requests
go func(sub *Subscription) {
for {
err := faye.requestSubscription(sub.channel)
// if it worked add it back to the list
if err == nil {
faye.mutex.Lock()
defer faye.mutex.Unlock()
faye.subscriptions = append(faye.subscriptions, sub)
faye.log.Debugf("Resubscribed to %s", sub.channel)
return
}
time.Sleep(500 * time.Millisecond)
}
}(sub)
}
}
// requests a subscription from the server and returns error if the request failed
func (faye *FayeClient) requestSubscription(channel string) error {
if err := faye.connectIfNotConnected(); err != nil {
return err
}
msg := faye.newMessage("/meta/subscribe")
msg.Subscription = channel
// TODO: check if the protocol allows a subscribe during an active connect request
response, _, err := faye.send(msg)
if err != nil {
return err
}
go faye.handleAdvice(response.Advice())
if !response.OK() {
// TODO: put more information in the error message about why it failed
errmsg := "Response was unsuccessful: "
if err != nil {
errmsg += err.Error()
}
if response.HasError() {
errmsg += " / " + response.Error()
}
reserr := errors.New(errmsg)
return reserr
}
return nil
}
func (faye *FayeClient) newMessage(channel string) *message {
return &message{
ClientID: faye.clientID,
Channel: channel,
}
}
// handles a response from the server
func (faye *FayeClient) handleMessages(msgs []Message) {
for _, message := range msgs {
faye.runExtensions("in", message)
for _, subscription := range faye.subscriptions {
matched, _ := filepath.Match(subscription.channel, message.Channel())
if matched {
go func() { subscription.msgChan <- message }()
}
}
}
}
// handles advice from the server
func (faye *FayeClient) handleAdvice(advice Advice) {
faye.mutex.Lock()
defer faye.mutex.Unlock()
if advice.Reconnect() != "" {
interval := advice.Interval()
switch advice.Reconnect() {
case RETRY:
if interval > 0 {
faye.nextHandshake = int64(time.Duration(time.Now().Unix()) + (time.Duration(interval) * time.Millisecond))
}
case HANDSHAKE:
faye.state = UNCONNECTED // force a handshake on the next request
if interval > 0 {
faye.nextHandshake = int64(time.Duration(time.Now().Unix()) + (time.Duration(interval) * time.Millisecond))
}
case NONE:
faye.state = DISCONNECTED
faye.log.Errorf("GTFO: Server advised not to reconnect :(")
}
}
}
// connects to the server and waits for a response. Will block if it is waiting
// for the nextRetry time as advised by the server. This locks the connectMutex
// so other connections can't go through until the
func (faye *FayeClient) connect() {
faye.connectMutex.Lock()
defer faye.connectMutex.Unlock()
msg := faye.newMessage("/meta/connect")
msg.ConnectionType = faye.transport.connectionType()
response, messages, err := faye.send(msg)
if err != nil {
faye.log.Errorf("Error while sending connect request: %s", err)
return
}
go faye.handleAdvice(response.Advice())
if response.OK() {
go faye.handleMessages(messages)
} else {
faye.log.Errorf("Error in response to connect request: %s", response.Error())
}
}
// wraps the call to transport.send()
func (faye *FayeClient) send(msg *message) (Response, []Message, error) {
if msg.ClientID == "" && msg.Channel != "/meta/handshake" && faye.clientID != "" {
msg.ClientID = faye.clientID
}
message := Message(msgWrapper{msg})
faye.runExtensions("out", message)
if message.HasError() {
return nil, []Message{}, message // Message has Error() so can be returned as an error
}
dec, err := faye.transport.send(message)
if err != nil {
err = fmt.Errorf("Error transporting message: %s", err)
faye.log.Errorf("%s", err)
return nil, []Message{}, err
}
r, m, err := decodeResponse(dec)
if err != nil {
faye.log.Errorf("Failed to decode response: %s", err)
}
if r != nil {
faye.runExtensions("in", r.(Message))
}
return r, m, err
}
// AddExtension adds an extension to the Faye Client
func (faye *FayeClient) AddExtension(extn Extension) {
faye.extns = append(faye.extns, extn)
}
func (faye *FayeClient) runExtensions(direction string, msg Message) {
for _, extn := range faye.extns {
faye.log.Debugf("Running extension %T %s", extn, direction)
switch direction {
case "out":
extn.Out(msg)
case "in":
extn.In(msg)
}
}
}
// Subscribe returns a channel to receive messages through
func (faye *FayeClient) Subscribe(channel string) (chan Message, error) {
if err := faye.requestSubscription(channel); err != nil {
return nil, err
}
msgChan := make(chan Message)
subscription := &Subscription{channel: channel, msgChan: msgChan}
faye.mutex.Lock()
defer faye.mutex.Unlock()
faye.subscriptions = append(faye.subscriptions, subscription)
return msgChan, nil
}
// WaitSubscribe will send a subscribe request and block until the connection was successful
func (faye *FayeClient) WaitSubscribe(channel string, optionalMsgChan ...chan Message) chan Message {
for {
if err := faye.requestSubscription(channel); err != nil {
faye.log.Errorf("[WaitSubscribe]: %s", err)
time.Sleep(1 * time.Second)
continue
}
break
}
msgChan := make(chan Message)
if len(optionalMsgChan) > 0 {
msgChan = optionalMsgChan[0]
}
subscription := &Subscription{channel: channel, msgChan: msgChan}
faye.mutex.Lock()
defer faye.mutex.Unlock()
faye.subscriptions = append(faye.subscriptions, subscription)
return msgChan
}
// Publish a message to the given channel
func (faye *FayeClient) Publish(channel string, data map[string]interface{}) error {
if err := faye.connectIfNotConnected(); err != nil {
return err
}
msg := faye.newMessage(channel)
msg.Data = data
response, _, err := faye.send(msg)
if err != nil {
return err
}
go faye.handleAdvice(response.Advice())
if !response.OK() {
return fmt.Errorf("Response was not successful")
}
return nil
}
// Listen starts listening for subscription requests from the server. It is
// blocking but can safely run in it's own goroutine.
func (faye *FayeClient) Listen() {
for {
if err := faye.connectIfNotConnected(); err != nil {
faye.log.Errorf("Handshake failed: %s", err)
time.Sleep(5 * time.Second)
}
for {
if faye.state != CONNECTED {
break
}
// wait to retry if we were told to
if faye.nextRetry > time.Now().Unix() {
sleepFor := faye.nextRetry - time.Now().Unix()
if sleepFor > 0 {
// fmt.Println("Waiting for", sleepFor, "seconds before connecting")
time.Sleep(time.Duration(sleepFor) * time.Second)
}
}
faye.connect()
}
}
}
// RegisterTransports allows for the dynamic loading of different transports
// and the most suitable one will be selected
func RegisterTransports(transports []Transport) {
registeredTransports = transports
}