forked from philippseith/signalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
633 lines (584 loc) · 16.2 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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package signalr
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/go-kit/log"
)
// ClientState is the state of the client.
type ClientState int
// Client states
//
// ClientCreated
//
// The Client has been created and is not started yet.
//
// ClientConnecting
//
// The Client has been started and is negotiating the connection.
//
// ClientConnected
//
// The Client has successfully negotiated the connection and can send and receive messages.
//
// ClientClosed
//
// The Client is not able to send and receive messages anymore and has to be started again to be able to.
const (
ClientCreated ClientState = iota
ClientConnecting
ClientConnected
ClientClosed
)
// Client is the signalR connection used on the client side.
//
// Start()
//
// Start starts the client loop. After starting the client, the interaction with a server can be started.
// The client loop will run until the server closes the connection. If WithConnector is used, Start will
// start a new loop. To end the loop from the client side, the context passed to NewClient has to be canceled
// or the Stop function has to be called.
//
// Stop()
//
// Stop stops the client loop. This is an alternative to using a cancelable context on NewClient.
//
// State() ClientState
//
// State returns the current client state.
// When WithConnector is set and the server allows reconnection, the client switches to ClientConnecting
// and tries to reach ClientConnected after the last connection has ended.
//
// ObserveStateChanged(chan ClientState) context.CancelFunc
//
// ObserveStateChanged pushes a new item != nil to the channel when State has changed.
// The returned CancelFunc ends the observation and closes the channel.
//
// Err() error
//
// Err returns the last error occurred while running the client.
// When the client goes to ClientConnecting, Err is set to nil.
//
// WaitForState(ctx context.Context, waitFor ClientState) <-chan error
//
// WaitForState returns a channel for waiting on the Client to reach a specific ClientState.
// The channel either returns an error if ctx or the client has been canceled.
// or nil if the ClientState waitFor was reached.
//
// Invoke(method string, arguments ...interface{}) <-chan InvokeResult
//
// Invoke invokes a method on the server and returns a channel wich will return the InvokeResult.
// When failing, InvokeResult.Error contains the client side error.
//
// Send(method string, arguments ...interface{}) <-chan error
//
// Send invokes a method on the server but does not return a result from the server but only a channel,
// which might contain a client side error occurred while sending.
//
// PullStream(method string, arguments ...interface{}) <-chan InvokeResult
//
// PullStream invokes a streaming method on the server and returns a channel which delivers the stream items.
// For more info about Streaming see https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md#streaming
//
// PushStreams(method string, arguments ...interface{}) <-chan error
//
// PushStreams pushes all items received from its arguments of type channel to the server (Upload Streaming).
// PushStreams does not support server methods that return a channel.
// For more info about Upload Streaming see https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md#upload-streaming
type Client interface {
Party
Start()
Stop()
State() ClientState
ObserveStateChanged(chan ClientState) context.CancelFunc
Err() error
WaitForState(ctx context.Context, waitFor ClientState) <-chan error
Invoke(method string, arguments ...interface{}) <-chan InvokeResult
Send(method string, arguments ...interface{}) <-chan error
PullStream(method string, arguments ...interface{}) <-chan InvokeResult
PushStreams(method string, arguments ...interface{}) <-chan InvokeResult
}
var ErrUnableToConnect = errors.New("neither WithConnection nor WithConnector option was given")
// NewClient builds a new Client.
// When ctx is canceled, the client loop and a possible auto reconnect loop are ended.
func NewClient(ctx context.Context, options ...func(Party) error) (Client, error) {
var cancelFunc context.CancelFunc
ctx, cancelFunc = context.WithCancel(ctx)
info, dbg := buildInfoDebugLogger(log.NewLogfmtLogger(os.Stderr), true)
c := &client{
state: ClientCreated,
stateChangeChans: make([]chan ClientState, 0),
format: "json",
partyBase: newPartyBase(ctx, info, dbg),
lastID: -1,
backoffFactory: func() backoff.BackOff { return backoff.NewExponentialBackOff() },
cancelFunc: cancelFunc,
}
for _, option := range options {
if option != nil {
if err := option(c); err != nil {
return nil, err
}
}
}
// Wrap logging with timestamps
info, dbg = c.loggers()
c.setLoggers(
log.WithPrefix(info, "ts", log.DefaultTimestampUTC),
log.WithPrefix(dbg, "ts", log.DefaultTimestampUTC),
)
if c.conn == nil && c.connectionFactory == nil {
return nil, ErrUnableToConnect
}
return c, nil
}
type client struct {
partyBase
mx sync.RWMutex
conn Connection
connectionFactory func() (Connection, error)
state ClientState
stateChangeChans []chan ClientState
err error
format string
loop *loop
receiver interface{}
lastID int64
backoffFactory func() backoff.BackOff
cancelFunc context.CancelFunc
}
func (c *client) Start() {
c.setState(ClientConnecting)
boff := c.backoffFactory()
go func() {
for {
c.setErr(nil)
// Listen for state change to ClientConnected and signal backoff Reset then.
stateChangeChan := make(chan ClientState, 1)
var connected atomic.Value
connected.Store(false)
cancelObserve := c.ObserveStateChanged(stateChangeChan)
go func() {
for range stateChangeChan {
if c.State() == ClientConnected {
connected.Store(true)
return
}
}
}()
// RUN!
err := c.run()
if err != nil {
_ = c.info.Log("connect", fmt.Sprintf("%v", err))
c.setErr(err)
}
shouldEnd := c.shouldClientEnd()
cancelObserve()
if shouldEnd {
return
}
// When the client has connected, BackOff should be reset
if connected.Load().(bool) {
boff.Reset()
}
// Reconnect after BackOff
nextBackoff := boff.NextBackOff()
// Check for exceeded backoff
if nextBackoff == backoff.Stop {
c.setErr(errors.New("backoff exceeded"))
return
}
select {
case <-time.After(nextBackoff):
case <-c.ctx.Done():
return
}
c.setState(ClientConnecting)
}
}()
}
func (c *client) Stop() {
if c.cancelFunc != nil {
c.cancelFunc()
}
c.setState(ClientClosed)
}
func (c *client) run() error {
// negotiate and so on
protocol, err := c.setupConnectionAndProtocol()
if err != nil {
return err
}
loop := newLoop(c, c.conn, protocol)
c.mx.Lock()
c.loop = loop
c.mx.Unlock()
// Broadcast when loop is connected
isLoopConnected := make(chan struct{}, 1)
go func() {
<-isLoopConnected
c.setState(ClientConnected)
}()
// Run the loop
err = loop.Run(isLoopConnected)
if err == nil {
err = loop.hubConn.Close("", false) // allowReconnect value is ignored as servers never initiate a connection
}
// Reset conn to allow reconnecting
c.mx.Lock()
c.conn = nil
c.mx.Unlock()
return err
}
func (c *client) shouldClientEnd() bool {
// Canceled?
if c.ctx.Err() != nil {
c.setErr(c.ctx.Err())
c.setState(ClientClosed)
return true
}
// Reconnecting not possible
if c.connectionFactory == nil {
c.setState(ClientClosed)
return true
}
// Reconnecting not allowed
if c.loop != nil && c.loop.closeMessage != nil && !c.loop.closeMessage.AllowReconnect {
c.setState(ClientClosed)
return true
}
return false
}
func (c *client) setupConnectionAndProtocol() (hubProtocol, error) {
return func() (hubProtocol, error) {
c.mx.Lock()
defer c.mx.Unlock()
if c.conn == nil {
if c.connectionFactory == nil {
return nil, ErrUnableToConnect
}
var err error
c.conn, err = c.connectionFactory()
if err != nil {
return nil, err
}
}
// Pass maximum receive message size to a potential websocket connection
if wsConn, ok := c.conn.(*webSocketConnection); ok {
wsConn.conn.SetReadLimit(int64(c.maximumReceiveMessageSize()))
}
protocol, err := c.processHandshake()
if err != nil {
return nil, err
}
return protocol, nil
}()
}
func (c *client) State() ClientState {
c.mx.RLock()
defer c.mx.RUnlock()
return c.state
}
func (c *client) setState(state ClientState) {
c.mx.Lock()
defer c.mx.Unlock()
c.state = state
_ = c.dbg.Log("state", state)
for _, ch := range c.stateChangeChans {
go func(ch chan ClientState, state ClientState) {
c.mx.Lock()
defer c.mx.Unlock()
for _, cch := range c.stateChangeChans {
if cch == ch {
select {
case ch <- state:
case <-c.ctx.Done():
}
}
}
}(ch, state)
}
}
func (c *client) ObserveStateChanged(ch chan ClientState) context.CancelFunc {
c.mx.Lock()
defer c.mx.Unlock()
c.stateChangeChans = append(c.stateChangeChans, ch)
return func() {
c.cancelObserveStateChanged(ch)
}
}
func (c *client) cancelObserveStateChanged(ch chan ClientState) {
c.mx.Lock()
defer c.mx.Unlock()
for i, cch := range c.stateChangeChans {
if cch == ch {
c.stateChangeChans = append(c.stateChangeChans[:i], c.stateChangeChans[i+1:]...)
close(ch)
break
}
}
}
func (c *client) Err() error {
c.mx.RLock()
defer c.mx.RUnlock()
return c.err
}
func (c *client) setErr(err error) {
c.mx.Lock()
defer c.mx.Unlock()
c.err = err
}
func (c *client) WaitForState(ctx context.Context, waitFor ClientState) <-chan error {
ch := make(chan error, 1)
if c.waitingIsOver(waitFor, ch) {
close(ch)
return ch
}
stateCh := make(chan ClientState, 1)
cancel := c.ObserveStateChanged(stateCh)
go func(waitFor ClientState) {
defer close(ch)
defer cancel()
if c.waitingIsOver(waitFor, ch) {
return
}
for {
select {
case <-stateCh:
if c.waitingIsOver(waitFor, ch) {
return
}
case <-ctx.Done():
ch <- ctx.Err()
return
case <-c.context().Done():
ch <- fmt.Errorf("client canceled: %w", c.context().Err())
return
}
}
}(waitFor)
return ch
}
func (c *client) waitingIsOver(waitFor ClientState, ch chan<- error) bool {
switch c.State() {
case waitFor:
return true
case ClientCreated:
ch <- errors.New("client not started. Call client.Start() before using it")
return true
case ClientClosed:
ch <- fmt.Errorf("client closed. %w", c.Err())
return true
}
return false
}
func (c *client) Invoke(method string, arguments ...interface{}) <-chan InvokeResult {
ch := make(chan InvokeResult, 1)
go func() {
if err := <-c.waitForConnected(); err != nil {
ch <- InvokeResult{Error: err}
close(ch)
return
}
id := c.loop.GetNewID()
resultCh, errCh := c.loop.invokeClient.newInvocation(id)
irCh := newInvokeResultChan(c.context(), resultCh, errCh)
if err := c.loop.hubConn.SendInvocation(id, method, arguments); err != nil {
c.loop.invokeClient.deleteInvocation(id)
ch <- InvokeResult{Error: err}
close(ch)
return
}
go func() {
for ir := range irCh {
ch <- ir
}
close(ch)
}()
}()
return ch
}
func (c *client) Send(method string, arguments ...interface{}) <-chan error {
errCh := make(chan error, 1)
go func() {
if err := <-c.waitForConnected(); err != nil {
errCh <- err
close(errCh)
return
}
id := c.loop.GetNewID()
_, sendErrCh := c.loop.invokeClient.newInvocation(id)
if err := c.loop.hubConn.SendInvocation(id, method, arguments); err != nil {
c.loop.invokeClient.deleteInvocation(id)
errCh <- err
close(errCh)
return
}
go func() {
for ir := range sendErrCh {
errCh <- ir
}
close(errCh)
}()
}()
return errCh
}
func (c *client) PullStream(method string, arguments ...interface{}) <-chan InvokeResult {
irCh := make(chan InvokeResult, 1)
go func() {
if err := <-c.waitForConnected(); err != nil {
irCh <- InvokeResult{Error: err}
close(irCh)
return
}
pullCh := c.loop.PullStream(method, c.loop.GetNewID(), arguments...)
go func() {
for ir := range pullCh {
irCh <- ir
if ir.Error != nil {
break
}
}
close(irCh)
}()
}()
return irCh
}
func (c *client) PushStreams(method string, arguments ...interface{}) <-chan InvokeResult {
irCh := make(chan InvokeResult, 1)
go func() {
if err := <-c.waitForConnected(); err != nil {
irCh <- InvokeResult{Error: err}
close(irCh)
return
}
pushCh, err := c.loop.PushStreams(method, c.loop.GetNewID(), arguments...)
if err != nil {
irCh <- InvokeResult{Error: err}
close(irCh)
return
}
go func() {
for ir := range pushCh {
irCh <- ir
}
close(irCh)
}()
}()
return irCh
}
func (c *client) waitForConnected() <-chan error {
return c.WaitForState(context.Background(), ClientConnected)
}
func createResultChansWithError(ctx context.Context, err error) (<-chan InvokeResult, chan error) {
resultCh := make(chan interface{}, 1)
errCh := make(chan error, 1)
errCh <- err
invokeResultChan := newInvokeResultChan(ctx, resultCh, errCh)
close(errCh)
close(resultCh)
return invokeResultChan, errCh
}
func (c *client) onConnected(hubConnection) {}
func (c *client) onDisconnected(hubConnection) {}
func (c *client) invocationTarget(hubConnection) interface{} {
return c.receiver
}
func (c *client) allowReconnect() bool {
return false // Servers don't care?
}
func (c *client) prefixLoggers(connectionID string) (info StructuredLogger, dbg StructuredLogger) {
if c.receiver == nil {
return log.WithPrefix(c.info, "ts", log.DefaultTimestampUTC, "class", "Client", "connection", connectionID),
log.WithPrefix(c.dbg, "ts", log.DefaultTimestampUTC, "class", "Client", "connection", connectionID)
}
var t reflect.Type = nil
switch reflect.ValueOf(c.receiver).Kind() {
case reflect.Ptr:
t = reflect.ValueOf(c.receiver).Elem().Type()
case reflect.Struct:
t = reflect.ValueOf(c.receiver).Type()
}
return log.WithPrefix(c.info, "ts", log.DefaultTimestampUTC,
"class", "Client",
"connection", connectionID,
"hub", t),
log.WithPrefix(c.dbg, "ts", log.DefaultTimestampUTC,
"class", "Client",
"connection", connectionID,
"hub", t)
}
func (c *client) processHandshake() (hubProtocol, error) {
if err := c.sendHandshakeRequest(); err != nil {
return nil, err
}
return c.receiveHandshakeResponse()
}
func (c *client) sendHandshakeRequest() error {
info, dbg := c.prefixLoggers(c.conn.ConnectionID())
request := fmt.Sprintf("{\"protocol\":\"%v\",\"version\":1}\u001e", c.format)
ctx, cancelWrite := context.WithTimeout(c.context(), c.HandshakeTimeout())
defer cancelWrite()
_, err := ReadWriteWithContext(ctx,
func() (int, error) {
return c.conn.Write([]byte(request))
}, func() {})
if err != nil {
_ = info.Log(evt, "handshake sent", "msg", request, "error", err)
return err
}
_ = dbg.Log(evt, "handshake sent", "msg", request)
return nil
}
func (c *client) receiveHandshakeResponse() (hubProtocol, error) {
info, dbg := c.prefixLoggers(c.conn.ConnectionID())
ctx, cancelRead := context.WithTimeout(c.context(), c.HandshakeTimeout())
defer cancelRead()
readJSONFramesChan := make(chan []interface{}, 1)
go func() {
var remainBuf bytes.Buffer
rawHandshake, err := readJSONFrames(c.conn, &remainBuf)
readJSONFramesChan <- []interface{}{rawHandshake, err}
}()
select {
case result := <-readJSONFramesChan:
if result[1] != nil {
return nil, result[1].(error)
}
rawHandshake := result[0].([][]byte)
response := handshakeResponse{}
if err := json.Unmarshal(rawHandshake[0], &response); err != nil {
// Malformed handshake
_ = info.Log(evt, "handshake received", "msg", string(rawHandshake[0]), "error", err)
return nil, err
} else {
if response.Error != "" {
_ = info.Log(evt, "handshake received", "error", response.Error)
return nil, errors.New(response.Error)
}
_ = dbg.Log(evt, "handshake received", "msg", fmtMsg(response))
var protocol hubProtocol
switch c.format {
case "json":
protocol = &jsonHubProtocol{}
case "messagepack":
protocol = &messagePackHubProtocol{}
}
if protocol != nil {
_, pDbg := c.loggers()
protocol.setDebugLogger(pDbg)
}
return protocol, nil
}
case <-ctx.Done():
return nil, ctx.Err()
}
}