-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
589 lines (503 loc) · 16.4 KB
/
subscription.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
package graphql
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/google/uuid"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
// Subscription transport follow Apollo's subscriptions-transport-ws protocol specification
// https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
// OperationMessageType
type OperationMessageType string
const (
// Client sends this message after plain websocket connection to start the communication with the server
GQL_CONNECTION_INIT OperationMessageType = "connection_init"
// The server may responses with this message to the GQL_CONNECTION_INIT from client, indicates the server rejected the connection.
GQL_CONNECTION_ERROR OperationMessageType = "conn_err"
// Client sends this message to execute GraphQL operation
GQL_START OperationMessageType = "start"
// Client sends this message in order to stop a running GraphQL operation execution (for example: unsubscribe)
GQL_STOP OperationMessageType = "stop"
// Server sends this message upon a failing operation, before the GraphQL execution, usually due to GraphQL validation errors (resolver errors are part of GQL_DATA message, and will be added as errors array)
GQL_ERROR OperationMessageType = "error"
// The server sends this message to transfter the GraphQL execution result from the server to the client, this message is a response for GQL_START message.
GQL_DATA OperationMessageType = "data"
// Server sends this message to indicate that a GraphQL operation is done, and no more data will arrive for the specific operation.
GQL_COMPLETE OperationMessageType = "complete"
// Server message that should be sent right after each GQL_CONNECTION_ACK processed and then periodically to keep the client connection alive.
// The client starts to consider the keep alive message only upon the first received keep alive message from the server.
GQL_CONNECTION_KEEP_ALIVE OperationMessageType = "ka"
// The server may responses with this message to the GQL_CONNECTION_INIT from client, indicates the server accepted the connection. May optionally include a payload.
GQL_CONNECTION_ACK OperationMessageType = "connection_ack"
// Client sends this message to terminate the connection.
GQL_CONNECTION_TERMINATE OperationMessageType = "connection_terminate"
// Unknown operation type, for logging only
GQL_UNKNOWN OperationMessageType = "unknown"
// Internal status, for logging only
GQL_INTERNAL OperationMessageType = "internal"
)
type OperationMessage struct {
ID string `json:"id,omitempty"`
Type OperationMessageType `json:"type"`
Payload json.RawMessage `json:"payload,omitempty"`
}
func (om OperationMessage) String() string {
bs, _ := json.Marshal(om)
return string(bs)
}
// WebsocketHandler abstracts WebSocket connecton functions
// ReadJSON and WriteJSON data of a frame from the WebSocket connection.
// Close the WebSocket connection.
type WebsocketConn interface {
ReadJSON(v interface{}) error
WriteJSON(v interface{}) error
Close() error
// SetReadLimit sets the maximum size in bytes for a message read from the peer. If a
// message exceeds the limit, the connection sends a close message to the peer
// and returns ErrReadLimit to the application.
SetReadLimit(limit int64)
}
type handlerFunc func(data *json.RawMessage, err error) error
type subscription struct {
query string
variables map[string]interface{}
handler func(data *json.RawMessage, err error)
started Boolean
}
// SubscriptionClient is a GraphQL subscription client.
type SubscriptionClient struct {
url string
conn WebsocketConn
connectionParams map[string]interface{}
context context.Context
subscriptions map[string]*subscription
cancel context.CancelFunc
subscribersMu sync.Mutex
timeout time.Duration
isRunning Boolean
readLimit int64 // max size of response message. Default 10 MB
log func(args ...interface{})
createConn func(sc *SubscriptionClient) (WebsocketConn, error)
retryTimeout time.Duration
onConnected func()
onDisconnected func()
onError func(sc *SubscriptionClient, err error) error
errorChan chan error
disabledLogTypes []OperationMessageType
}
func NewSubscriptionClient(url string) *SubscriptionClient {
return &SubscriptionClient{
url: url,
timeout: time.Minute,
readLimit: 10 * 1024 * 1024, // set default limit 10MB
subscriptions: make(map[string]*subscription),
createConn: newWebsocketConn,
retryTimeout: time.Minute,
errorChan: make(chan error),
}
}
// GetURL returns GraphQL server's URL
func (sc *SubscriptionClient) GetURL() string {
return sc.url
}
// GetContext returns current context of subscription client
func (sc *SubscriptionClient) GetContext() context.Context {
return sc.context
}
// GetContext returns write timeout of websocket client
func (sc *SubscriptionClient) GetTimeout() time.Duration {
return sc.timeout
}
// WithWebSocket replaces customized websocket client constructor
// In default, subscription client uses https://github.com/nhooyr/websocket
func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (WebsocketConn, error)) *SubscriptionClient {
sc.createConn = fn
return sc
}
// WithConnectionParams updates connection params for sending to server through GQL_CONNECTION_INIT event
// It's usually used for authentication handshake
func (sc *SubscriptionClient) WithConnectionParams(params map[string]interface{}) *SubscriptionClient {
sc.connectionParams = params
return sc
}
// WithTimeout updates write timeout of websocket client
func (sc *SubscriptionClient) WithTimeout(timeout time.Duration) *SubscriptionClient {
sc.timeout = timeout
return sc
}
// WithRetryTimeout updates reconnecting timeout. When the websocket server was stopped, the client will retry connecting every second until timeout
func (sc *SubscriptionClient) WithRetryTimeout(timeout time.Duration) *SubscriptionClient {
sc.retryTimeout = timeout
return sc
}
// WithLog sets loging function to print out received messages. By default, nothing is printed
func (sc *SubscriptionClient) WithLog(logger func(args ...interface{})) *SubscriptionClient {
sc.log = logger
return sc
}
// WithoutLogTypes these operation types won't be printed
func (sc *SubscriptionClient) WithoutLogTypes(types ...OperationMessageType) *SubscriptionClient {
sc.disabledLogTypes = types
return sc
}
// WithReadLimit set max size of response message
func (sc *SubscriptionClient) WithReadLimit(limit int64) *SubscriptionClient {
sc.readLimit = limit
return sc
}
// OnConnected event is triggered when there is any connection error. This is bottom exception handler level
// If this function is empty, or returns nil, the error is ignored
// If returns error, the websocket connection will be terminated
func (sc *SubscriptionClient) OnError(onError func(sc *SubscriptionClient, err error) error) *SubscriptionClient {
sc.onError = onError
return sc
}
// OnConnected event is triggered when the websocket connected to GraphQL server sucessfully
func (sc *SubscriptionClient) OnConnected(fn func()) *SubscriptionClient {
sc.onConnected = fn
return sc
}
// OnDisconnected event is triggered when the websocket server was stil down after retry timeout
func (sc *SubscriptionClient) OnDisconnected(fn func()) *SubscriptionClient {
sc.onDisconnected = fn
return sc
}
func (sc *SubscriptionClient) setIsRunning(value Boolean) {
sc.subscribersMu.Lock()
sc.isRunning = value
sc.subscribersMu.Unlock()
}
func (sc *SubscriptionClient) init() error {
now := time.Now()
ctx, cancel := context.WithCancel(context.Background())
sc.context = ctx
sc.cancel = cancel
for {
var err error
var conn WebsocketConn
// allow custom websocket client
if sc.conn == nil {
conn, err = newWebsocketConn(sc)
if err == nil {
sc.conn = conn
}
}
if err == nil {
sc.conn.SetReadLimit(sc.readLimit)
// send connection init event to the server
err = sc.sendConnectionInit()
}
if err == nil {
return nil
}
if now.Add(sc.retryTimeout).Before(time.Now()) {
if sc.onDisconnected != nil {
sc.onDisconnected()
}
return err
}
sc.printLog(err.Error()+". retry in second....", GQL_INTERNAL)
time.Sleep(time.Second)
}
}
func (sc *SubscriptionClient) printLog(message interface{}, opType OperationMessageType) {
if sc.log == nil {
return
}
for _, ty := range sc.disabledLogTypes {
if ty == opType {
return
}
}
sc.log(message)
}
func (sc *SubscriptionClient) sendConnectionInit() (err error) {
var bParams []byte = nil
if sc.connectionParams != nil {
bParams, err = json.Marshal(sc.connectionParams)
if err != nil {
return
}
}
// send connection_init event to the server
msg := OperationMessage{
Type: GQL_CONNECTION_INIT,
Payload: bParams,
}
sc.printLog(msg, GQL_CONNECTION_INIT)
return sc.conn.WriteJSON(msg)
}
// Subscribe sends start message to server and open a channel to receive data.
// The handler callback function will receive raw message data or error. If the call return error, onError event will be triggered
// The function returns subscription ID and error. You can use subscription ID to unsubscribe the subscription
func (sc *SubscriptionClient) Subscribe(v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error) (string, error) {
return sc.do(v, variables, handler, "")
}
// NamedSubscribe sends start message to server and open a channel to receive data, with operation name
func (sc *SubscriptionClient) NamedSubscribe(name string, v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error) (string, error) {
return sc.do(v, variables, handler, name)
}
func (sc *SubscriptionClient) do(v interface{}, variables map[string]interface{}, handler func(message *json.RawMessage, err error) error, name string) (string, error) {
id := uuid.New().String()
query := constructSubscription(v, variables, name)
sub := subscription{
query: query,
variables: variables,
handler: sc.wrapHandler(handler),
}
// if the websocket client is running, start subscription immediately
if sc.isRunning {
if err := sc.startSubscription(id, &sub); err != nil {
return "", err
}
}
sc.subscribersMu.Lock()
sc.subscriptions[id] = &sub
sc.subscribersMu.Unlock()
return id, nil
}
// Subscribe sends start message to server and open a channel to receive data
func (sc *SubscriptionClient) startSubscription(id string, sub *subscription) error {
if sub == nil || sub.started {
return nil
}
in := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}{
Query: sub.query,
Variables: sub.variables,
}
payload, err := json.Marshal(in)
if err != nil {
return err
}
// send stop message to the server
msg := OperationMessage{
ID: id,
Type: GQL_START,
Payload: payload,
}
sc.printLog(msg, GQL_START)
if err := sc.conn.WriteJSON(msg); err != nil {
return err
}
sub.started = true
return nil
}
func (sc *SubscriptionClient) wrapHandler(fn handlerFunc) func(data *json.RawMessage, err error) {
return func(data *json.RawMessage, err error) {
if errValue := fn(data, err); errValue != nil {
sc.errorChan <- errValue
}
}
}
// Run start websocket client and subscriptions. If this function is run with goroutine, it can be stopped after closed
func (sc *SubscriptionClient) Run() error {
if err := sc.init(); err != nil {
return fmt.Errorf("retry timeout. exiting...")
}
// lazily start subscriptions
for k, v := range sc.subscriptions {
if err := sc.startSubscription(k, v); err != nil {
sc.Unsubscribe(k)
return err
}
}
sc.setIsRunning(true)
for sc.isRunning {
select {
case <-sc.context.Done():
return nil
case e := <-sc.errorChan:
if sc.onError != nil {
if err := sc.onError(sc, e); err != nil {
return err
}
}
default:
var message OperationMessage
if err := sc.conn.ReadJSON(&message); err != nil {
// manual EOF check
if err == io.EOF || strings.Contains(err.Error(), "EOF") {
return sc.Reset()
}
closeStatus := websocket.CloseStatus(err)
if closeStatus == websocket.StatusNormalClosure {
// close event from websocket client, exiting...
return nil
}
if closeStatus != -1 {
sc.printLog(fmt.Sprintf("%s. Retry connecting...", err), GQL_INTERNAL)
return sc.Reset()
}
if sc.onError != nil {
if err = sc.onError(sc, err); err != nil {
return err
}
}
continue
}
switch message.Type {
case GQL_ERROR:
sc.printLog(message, GQL_ERROR)
fallthrough
case GQL_DATA:
sc.printLog(message, GQL_DATA)
id, err := uuid.Parse(message.ID)
if err != nil {
continue
}
sub, ok := sc.subscriptions[id.String()]
if !ok {
continue
}
var out struct {
Data *json.RawMessage
Errors errors
//Extensions interface{} // Unused.
}
err = json.Unmarshal(message.Payload, &out)
if err != nil {
go sub.handler(nil, err)
continue
}
if len(out.Errors) > 0 {
go sub.handler(nil, out.Errors)
continue
}
go sub.handler(out.Data, nil)
case GQL_CONNECTION_ERROR:
sc.printLog(message, GQL_CONNECTION_ERROR)
case GQL_COMPLETE:
sc.printLog(message, GQL_COMPLETE)
sc.Unsubscribe(message.ID)
case GQL_CONNECTION_KEEP_ALIVE:
sc.printLog(message, GQL_CONNECTION_KEEP_ALIVE)
case GQL_CONNECTION_ACK:
sc.printLog(message, GQL_CONNECTION_ACK)
if sc.onConnected != nil {
sc.onConnected()
}
default:
sc.printLog(message, GQL_UNKNOWN)
}
}
}
// if the running status is false, stop retrying
if !sc.isRunning {
return nil
}
return sc.Reset()
}
// Unsubscribe sends stop message to server and close subscription channel
// The input parameter is subscription ID that is returned from Subscribe function
func (sc *SubscriptionClient) Unsubscribe(id string) error {
_, ok := sc.subscriptions[id]
if !ok {
return fmt.Errorf("subscription id %s doesn't not exist", id)
}
err := sc.stopSubscription(id)
sc.subscribersMu.Lock()
delete(sc.subscriptions, id)
sc.subscribersMu.Unlock()
return err
}
func (sc *SubscriptionClient) stopSubscription(id string) error {
if sc.conn != nil {
// send stop message to the server
msg := OperationMessage{
ID: id,
Type: GQL_STOP,
}
sc.printLog(msg, GQL_STOP)
if err := sc.conn.WriteJSON(msg); err != nil {
return err
}
}
return nil
}
func (sc *SubscriptionClient) terminate() error {
if sc.conn != nil {
// send terminate message to the server
msg := OperationMessage{
Type: GQL_CONNECTION_TERMINATE,
}
sc.printLog(msg, GQL_CONNECTION_TERMINATE)
return sc.conn.WriteJSON(msg)
}
return nil
}
// Reset restart websocket connection and subscriptions
func (sc *SubscriptionClient) Reset() error {
if !sc.isRunning {
return nil
}
for id, sub := range sc.subscriptions {
_ = sc.stopSubscription(id)
sub.started = false
}
if sc.conn != nil {
_ = sc.terminate()
_ = sc.conn.Close()
sc.conn = nil
}
sc.cancel()
return sc.Run()
}
// Close closes all subscription channel and websocket as well
func (sc *SubscriptionClient) Close() (err error) {
sc.setIsRunning(false)
for id := range sc.subscriptions {
if err = sc.Unsubscribe(id); err != nil {
sc.cancel()
return err
}
}
if sc.conn != nil {
_ = sc.terminate()
err = sc.conn.Close()
sc.conn = nil
}
sc.cancel()
return
}
// default websocket handler implementation using https://github.com/nhooyr/websocket
type websocketHandler struct {
ctx context.Context
timeout time.Duration
*websocket.Conn
}
func (wh *websocketHandler) WriteJSON(v interface{}) error {
ctx, cancel := context.WithTimeout(wh.ctx, wh.timeout)
defer cancel()
return wsjson.Write(ctx, wh.Conn, v)
}
func (wh *websocketHandler) ReadJSON(v interface{}) error {
ctx, cancel := context.WithTimeout(wh.ctx, wh.timeout)
defer cancel()
return wsjson.Read(ctx, wh.Conn, v)
}
func (wh *websocketHandler) Close() error {
return wh.Conn.Close(websocket.StatusNormalClosure, "close websocket")
}
func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) {
options := &websocket.DialOptions{
Subprotocols: []string{"graphql-ws"},
}
c, _, err := websocket.Dial(sc.GetContext(), sc.GetURL(), options)
if err != nil {
return nil, err
}
return &websocketHandler{
ctx: sc.GetContext(),
Conn: c,
timeout: sc.GetTimeout(),
}, nil
}