-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler_ws.go
560 lines (465 loc) · 14 KB
/
handler_ws.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
package bloxroute_sdk_go
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/sourcegraph/jsonrpc2"
"github.com/valyala/fastjson"
"github.com/bloXroute-Labs/gateway/v2/jsonrpc"
"github.com/bloXroute-Labs/gateway/v2/types"
"github.com/bloXroute-Labs/bloxroute-sdk-go/connection/ws"
)
const (
blockchainHeaderKey = "X-BloXroute-Blockchain"
sdkVersionHeaderKey = "X-BloXroute-SDK-Version"
languageHeaderKey = "X-BloXroute-Code-Language"
authHeaderKey = "Authorization"
requestWaitTimeout = time.Minute
unsubscribeTimeout = time.Second * 10
unsubscribeInitialInterval = time.Millisecond * 100
reconnectTimeout = time.Second * 60
reconnectInitialInterval = time.Millisecond * 100
closeTimeout = time.Millisecond * 200
)
var (
ErrNotConnected = errors.New("WS connection not established")
ErrNoResponse = errors.New("no response")
)
type wsHandler struct {
hst handlerSourceType
config *Config
conn ws.Conn
feeds map[types.FeedType]feed
subscriptions map[string]wsSubscription
pendingResponse map[jsonrpc2.ID]chan requestResponse
lock *sync.Mutex
stop chan struct{}
wg *sync.WaitGroup
readErr chan error
}
// requestResponse represents a response to either a normal request or
// a subscription request. If it is a request, Result will be set, otherwise
// if it is a subscription request, ID will be the subscription ID
type requestResponse struct {
ID string
Result []byte
Error *RPCError
}
// subscription represents a subscription to a feed
type wsSubscription struct {
callback CallbackFunc[any]
feed types.FeedType
subReq *jsonrpc2.Request
}
type feed struct {
subscriptionID string
}
// Type returns the WS handler type
func (h *wsHandler) Type() handlerSourceType {
return h.hst
}
// Subscribe subscribes to a feed
func (h *wsHandler) Subscribe(ctx context.Context, f types.FeedType, params any, callback CallbackFunc[any]) error {
raw, err := json.Marshal([]interface{}{f, params})
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
}
req := &jsonrpc2.Request{
ID: randomID(),
Method: string(jsonrpc.RPCSubscribe),
Params: (*json.RawMessage)(&raw),
}
resChan, err := h.subscribe(ctx, f, req)
if err != nil {
return err
}
_, err = h.waitSubscriptionResponse(ctx, resChan, f, req, callback)
return err
}
// Request sends a request via WS
func (h *wsHandler) Request(ctx context.Context, method jsonrpc.RPCRequestType, params any) (*json.RawMessage, error) {
raw, err := json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("failed to marshal params: %w", err)
}
req := &jsonrpc2.Request{
ID: randomID(),
Method: string(method),
Params: (*json.RawMessage)(&raw),
}
resChan, err := h.request(ctx, req)
if err != nil {
return nil, err
}
return h.waitRequestResponse(ctx, resChan, req)
}
// Close stops the read loop, unsubscribes from all feeds and closes the connection
func (h *wsHandler) Close() error {
close(h.stop)
// unsubscribe from all feeds and close the connection
unsubscribeRequest := &jsonrpc2.Request{
ID: randomID(),
Method: string(jsonrpc.RPCUnsubscribe),
}
ctx, cancel := context.WithTimeout(context.Background(), unsubscribeTimeout)
defer cancel()
err := errors.Join(h.conn.WriteJSON(ctx, unsubscribeRequest), h.conn.Close())
// this is workaround for the fact that the Read blocks until there is a message
// and some WS implementations don't support ctx cancellation
c := make(chan struct{})
go func() {
defer close(c)
h.wg.Wait()
}()
select {
case <-c:
case <-time.After(closeTimeout):
}
return err
}
// UnsubscribeRetry unsubscribes from a feed with retries
func (h *wsHandler) UnsubscribeRetry(f types.FeedType) error {
backOff := backoff.NewExponentialBackOff()
backOff.MaxElapsedTime = unsubscribeTimeout
backOff.InitialInterval = unsubscribeInitialInterval
fn := func() error {
return h.unsubscribe(f)
}
return backoff.Retry(fn, backOff)
}
// read starts reading messages via WS from gateway or cloud API and handles them.
// read blocks until the context is canceled, an error occurs or the client is closed.
func (h *wsHandler) read(ctx context.Context) {
defer h.wg.Done()
for {
select {
case <-h.stop:
return
case <-ctx.Done():
return
default:
message, err := h.conn.ReadMessage(ctx)
if err != nil {
select {
case <-h.stop:
// client is closed, so just return
return
default:
}
if !ws.IsWSClosedError(err) || !*h.config.Reconnect {
h.config.Logger.Errorf("failed to read message from WS: %s", err)
return
}
// connection is closed, try to reconnect
err := h.reconnect(ctx)
if err != nil {
h.config.Logger.Errorf("failed to reconnect to WS: %s", err)
continue
}
// try to re-subscribe to all feeds
go h.resubscribeAll(ctx)
continue
}
// double-check if message is nil
if message == nil {
continue
}
err = h.handleMessage(ctx, message)
if err != nil {
h.config.Logger.Errorf("failed to handle message: %s", err)
}
}
}
}
func (h *wsHandler) reconnect(ctx context.Context) error {
var urlString string
if h.config.WSCloudAPIURL != "" {
urlString = h.config.WSCloudAPIURL
} else {
urlString = h.config.WSGatewayURL
}
headers := http.Header{
authHeaderKey: []string{h.config.AuthHeader},
blockchainHeaderKey: []string{h.config.BlockchainNetwork},
sdkVersionHeaderKey: []string{buildVersion},
languageHeaderKey: []string{runtime.Version()},
}
conn, err := h.config.WSConnectFunc(ctx, urlString, headers, h.config.WSDialOptions)
if err != nil {
return fmt.Errorf("failed to connect to WS: %w", err)
}
h.conn = conn
return nil
}
func (h *wsHandler) handleMessage(ctx context.Context, message []byte) error {
var p fastjson.Parser
v, err := p.ParseBytes(message)
if err != nil {
return fmt.Errorf("failed to parse message: %w", err)
}
// check the subscription ID exists
id := jsonrpc2.ID{Str: string(v.GetStringBytes("id")), IsString: true}
h.lock.Lock()
resChan, ok := h.pendingResponse[id]
h.lock.Unlock()
if ok {
return h.handlePendingResponse(id, resChan, v)
}
method := v.GetStringBytes("method")
if string(method) != "subscribe" {
return nil
}
h.lock.Lock()
subscription, ok := h.subscriptions[string(v.GetStringBytes("params", "subscription"))]
h.lock.Unlock()
if !ok {
// subscription not found
return nil
}
var res any
switch subscription.feed {
case types.OnBlockFeed:
res = &OnBlockNotification{
Name: string(v.GetStringBytes("params", "result", "name")),
Response: string(v.GetStringBytes("params", "result", "response")),
BlockHeight: string(v.GetStringBytes("params", "result", "block_height")),
Tag: string(v.GetStringBytes("params", "result", "tag")),
}
case types.BDNBlocksFeed:
res = &OnBdnBlockNotification{}
err = json.Unmarshal(v.GetObject("params", "result").MarshalTo(nil), &res)
if err != nil {
err = fmt.Errorf("failed to unmarshal bdn block notification: %w", err)
}
case types.NewBlocksFeed:
res = &OnBdnBlockNotification{}
err = json.Unmarshal(v.GetObject("params", "result").MarshalTo(nil), &res)
if err != nil {
err = fmt.Errorf("failed to unmarshal new block notification: %w", err)
}
case types.NewTxsFeed, types.PendingTxsFeed:
res = &NewTxNotification{}
err = json.Unmarshal(v.GetObject("params", "result").MarshalTo(nil), &res)
if err != nil {
err = fmt.Errorf("failed to unmarshal new tx notification: %w", err)
}
case types.TransactionStatusFeed:
res = &OnTxStatusNotification{
TxHash: string(v.GetStringBytes("params", "result", "tx_hash")),
Status: string(v.GetStringBytes("params", "result", "status")),
}
case types.TxReceiptsFeed:
res = &OnTxReceiptNotification{}
err = json.Unmarshal(v.GetObject("params", "result").MarshalTo(nil), &res)
if err != nil {
err = fmt.Errorf("failed to unmarshal tx receipt notification: %w", err)
}
}
subscription.callback(ctx, err, res)
return nil
}
func (h *wsHandler) handlePendingResponse(id jsonrpc2.ID, resChan chan requestResponse, v *fastjson.Value) error {
defer func() {
close(resChan)
h.lock.Lock()
delete(h.pendingResponse, id)
h.lock.Unlock()
}()
errMessage := v.GetObject("error")
if errMessage != nil {
code, _ := errMessage.Get("code").Int64()
data := json.RawMessage(errMessage.Get("data").GetStringBytes())
select {
case resChan <- requestResponse{Error: &RPCError{Code: code, Message: errMessage.Get("message").String(), Data: &data}}:
return nil
default:
return nil
}
}
// when result is a string, it is a subscription ID for a subscription request
// otherwise it looks to be a response to a normal request
result := v.Get("result")
if result != nil {
subscriptionId := v.GetStringBytes("result")
if len(subscriptionId) > 0 {
select {
case resChan <- requestResponse{ID: string(subscriptionId)}:
return nil
default:
return nil
}
} else {
resBytes := result.MarshalTo(nil)
select {
case resChan <- requestResponse{Result: resBytes}:
return nil
default:
}
}
}
return nil
}
// subscribe subscribes to a feed
func (h *wsHandler) subscribe(ctx context.Context, f types.FeedType, subReq *jsonrpc2.Request) (chan requestResponse, error) {
h.lock.Lock()
defer h.lock.Unlock()
// check if already subscribed
_, ok := h.feeds[f]
if ok {
return nil, fmt.Errorf("already subscribed to %s", f)
}
// connect if not connected
if h.conn == nil {
return nil, ErrNotConnected
}
resChan := make(chan requestResponse, 1)
// add subscription
h.feeds[f] = feed{subscriptionID: ""}
h.pendingResponse[subReq.ID] = resChan
err := h.conn.WriteJSON(ctx, subReq)
if err != nil {
return nil, fmt.Errorf("failed to write subscribe request for %s feed: %w", f, err)
}
return resChan, nil
}
// waitSubscriptionResponse waits for a subscription response
func (h *wsHandler) waitSubscriptionResponse(ctx context.Context, resChan chan requestResponse, feedType types.FeedType, subReq *jsonrpc2.Request, callback CallbackFunc[any]) (string, error) {
wait := time.NewTimer(requestWaitTimeout)
select {
case <-ctx.Done():
return "", nil
case res, ok := <-resChan:
if !ok {
return "", nil
}
if res.Error != nil {
return "", res.Error
}
// add a subscription
h.lock.Lock()
defer h.lock.Unlock()
h.subscriptions[res.ID] = wsSubscription{subReq: subReq, callback: callback, feed: feedType}
h.feeds[feedType] = feed{subscriptionID: res.ID}
return res.ID, nil
case <-wait.C:
h.lock.Lock()
defer h.lock.Unlock()
delete(h.pendingResponse, subReq.ID)
delete(h.feeds, feedType)
return "", fmt.Errorf("didn't receive response for %s subscription request within %s", feedType, requestWaitTimeout)
}
}
// makes a single rpc request and expects a single response
func (h *wsHandler) request(ctx context.Context, req *jsonrpc2.Request) (chan requestResponse, error) {
h.lock.Lock()
defer h.lock.Unlock()
// connect if not connected
if h.conn == nil {
return nil, ErrNotConnected
}
resChan := make(chan requestResponse, 1)
h.pendingResponse[req.ID] = resChan
err := h.conn.WriteJSON(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to write request: %w", err)
}
return resChan, nil
}
// wait requestResponse waits for a request response and returns the response or error
func (h *wsHandler) waitRequestResponse(ctx context.Context, resChan chan requestResponse, req *jsonrpc2.Request) (*json.RawMessage, error) {
wait := time.NewTimer(requestWaitTimeout)
select {
case <-ctx.Done():
return nil, ctx.Err()
case res, ok := <-resChan:
if !ok {
return nil, ErrNoResponse
}
if res.Error != nil {
return nil, res.Error
}
result := json.RawMessage(res.Result)
return &result, nil
case <-wait.C:
h.lock.Lock()
defer h.lock.Unlock()
delete(h.pendingResponse, req.ID)
return nil, fmt.Errorf("request timed out")
}
}
func (h *wsHandler) resubscribeAll(ctx context.Context) {
h.lock.Lock()
subCopy := make([]wsSubscription, 0, len(h.subscriptions))
for _, subscription := range h.subscriptions {
subCopy = append(subCopy, subscription)
}
h.subscriptions = make(map[string]wsSubscription)
h.feeds = make(map[types.FeedType]feed)
h.lock.Unlock()
loop:
for _, subscription := range subCopy {
select {
case <-ctx.Done():
return
default:
// create new subscription ID
subscription.subReq.ID = randomID()
resChan, err := h.subscribe(ctx, subscription.feed, subscription.subReq)
if err != nil {
h.config.Logger.Errorf("failed to resubscribe to %s feed: %s", subscription.feed, err)
continue loop
}
_, err = h.waitSubscriptionResponse(ctx, resChan, subscription.feed, subscription.subReq, subscription.callback)
if err != nil {
h.config.Logger.Errorf("failed to resubscribe to %s feed: %s", subscription.feed, err)
}
}
}
}
func (h *wsHandler) unsubscribe(f types.FeedType) error {
h.lock.Lock()
defer h.lock.Unlock()
if h.conn == nil {
return backoff.Permanent(fmt.Errorf("connection is not established"))
}
_, ok := h.feeds[f]
if !ok {
// no need to unsubscribe
return nil
}
var subscriptionID string
for id, subscription := range h.subscriptions {
if subscription.feed != f {
continue
}
subscriptionID = id
break
}
if subscriptionID == "" {
return fmt.Errorf("no subscription ID is defined for %s yet", types.NewTxsFeed)
}
raw, err := json.Marshal([]interface{}{subscriptionID})
if err != nil {
return backoff.Permanent(fmt.Errorf("failed to marshal params: %w", err))
}
unsubscribeRequest := &jsonrpc2.Request{
ID: randomID(),
Method: string(jsonrpc.RPCUnsubscribe),
Params: (*json.RawMessage)(&raw),
}
ctx, cancel := context.WithTimeout(context.Background(), unsubscribeTimeout)
defer cancel()
err = h.conn.WriteJSON(ctx, unsubscribeRequest)
if err != nil {
return fmt.Errorf("failed to write unsubscribe request for %s feed: %w", f, err)
}
delete(h.feeds, f)
delete(h.subscriptions, subscriptionID)
return nil
}