-
Notifications
You must be signed in to change notification settings - Fork 45
/
wsconn.go
381 lines (310 loc) · 10.6 KB
/
wsconn.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
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eosws
import (
"context"
"encoding/json"
"fmt"
"reflect"
"sync"
"time"
"github.com/streamingfast/derr"
"github.com/dfuse-io/dfuse-eosio/eosws/wsmsg"
"github.com/streamingfast/dtracing"
"github.com/streamingfast/logging"
"github.com/streamingfast/shutter"
"github.com/gorilla/websocket"
"github.com/streamingfast/dauth/authenticator"
"github.com/streamingfast/dmetering"
"github.com/tidwall/gjson"
"go.opencensus.io/trace"
"go.uber.org/zap"
)
var DisabledWsMessage map[string]interface{}
// WSConn represents a single web socket connection.
type WSConn struct {
*shutter.Shutter
*WebsocketHandler
conn *websocket.Conn
//pipelines map[string]afterburner.Pipeline
listenerCancelers map[string]func() error
listenersLock sync.Mutex
emitLock sync.Mutex
creds authenticator.Credentials
Context context.Context
filesourceBlockRateLimit time.Duration
}
func NewWSConn(wshand *WebsocketHandler, conn *websocket.Conn, creds authenticator.Credentials, filesourceBlockRateLimit time.Duration, ctx context.Context) *WSConn {
// Each WS conn will have its own SubscribablePipeline ? Hooked into the main pipeline
// of the process, let's, for now, simply create a Joiner per socket
ws := &WSConn{
WebsocketHandler: wshand,
conn: conn,
creds: creds,
listenerCancelers: make(map[string]func() error),
Context: ctx,
filesourceBlockRateLimit: filesourceBlockRateLimit,
}
ws.Shutter = shutter.New()
ws.Shutter.OnTerminating(func(e error) {
_ = ws.conn.Close()
ws.ShutdownAllListeners()
TrackUserEvent(ws.Context, "ws_conn_close", "error", ws.Err())
})
return ws
}
func (ws *WSConn) handleHeartbeats() {
for {
time.Sleep(10 * time.Second)
if ws.IsTerminating() {
return
}
m := wsmsg.NewPing(time.Now().UTC())
ws.Emit(ws.Context, m)
}
}
func (ws *WSConn) RegisterListener(ctx context.Context, reqID string, canceler func() error) error {
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("registering listener", zap.String("req_id", reqID))
ws.listenersLock.Lock()
defer ws.listenersLock.Unlock()
if canceler == nil {
return fmt.Errorf("cannot register listener with null canceller")
}
if ws.IsTerminating() {
return WSAlreadyClosedError(ws.Context)
}
streamCount := len(ws.listenerCancelers)
if streamCount > ws.maxStreamCount {
return WSTooMuchStreamError(ws.Context, streamCount, ws.maxStreamCount)
}
if ws.listenerCancelers[reqID] != nil {
return WSStreamAlreadyExistError(ws.Context, reqID)
}
ws.listenerCancelers[reqID] = canceler
zlogger.Debug("added listener cancelers", zap.Int("new_count", len(ws.listenerCancelers)))
return nil
}
func (ws *WSConn) ShutdownListener(ctx context.Context, reqID string) error {
zlogger := logging.Logger(ctx, zlog)
zlogger.Info("shutting down listener", zap.String("req_id", reqID))
ws.listenersLock.Lock()
defer ws.listenersLock.Unlock()
canceler, ok := ws.listenerCancelers[reqID]
if !ok {
return WSStreamNotFoundError(ctx, reqID)
}
zlogger.Debug("invoking listener canceler")
if err := canceler(); err != nil {
return derr.Wrap(err, "listener canceler failed")
}
delete(ws.listenerCancelers, reqID)
zlogger.Debug("removed listener cancelers", zap.Int("new_count", len(ws.listenerCancelers)))
return nil
}
func (ws *WSConn) ShutdownAllListeners() {
ws.listenersLock.Lock()
defer ws.listenersLock.Unlock()
zlogger := logging.Logger(ws.Context, zlog)
for reqID, canceler := range ws.listenerCancelers {
if err := canceler(); err != nil {
zlogger.Warn("canceler for listener failed canceling", zap.Error(err))
}
delete(ws.listenerCancelers, reqID)
}
}
func (ws *WSConn) handleWSIncoming() {
for {
// msgType ignored, no distinction between binary and text messages
msgType, rawmsg, err := ws.conn.ReadMessage()
if err != nil {
if !ws.IsTerminating() { // not our concern if it is already shut down...
ws.Shutdown(err)
}
return
}
if msgType == websocket.BinaryMessage {
ws.EmitError(ws.Context, "", WSBinaryMessageUnsupportedError(ws.Context))
continue
}
ws.handleMessage(rawmsg)
}
}
func (ws *WSConn) handleMessage(rawMsg []byte) {
var inspect struct {
Type string `json:"type"`
ReqID string `json:"req_id,omitempty"`
Listen *bool `json:"listen,omitempty"`
Data interface{} `json:"data,omitempty"`
}
ctx := ws.Context
err := json.Unmarshal(rawMsg, &inspect)
if err != nil {
ws.EmitError(ctx, "", WSInvalidJSONMessageError(ctx, err))
return
}
if inspect.Type == "pong" {
return
}
if _, disabled := DisabledWsMessage[inspect.Type]; disabled {
ws.EmitError(ctx, inspect.ReqID, WSUnavailableMessageError(ctx, inspect.Type))
return
}
objType := wsmsg.IncomingMessageMap[inspect.Type]
if objType == nil {
ws.EmitError(ctx, inspect.ReqID, WSUnknownMessageError(ctx, inspect.Type))
return
}
obj := reflect.New(objType).Interface()
err = json.Unmarshal(rawMsg, &obj)
if err != nil {
ws.EmitError(ctx, inspect.ReqID, WSInvalidJSONMessageDataError(ctx, inspect.Type, err))
return
}
if validatorObj, ok := obj.(wsmsg.Validator); ok {
logging.Logger(ctx, zlog).Info("validating message")
if err := validatorObj.Validate(ctx); err != nil {
ws.EmitError(ctx, inspect.ReqID, WSMessageDataValidationError(ctx, err))
return
}
}
childCtx := ctx
if inMsg, ok := obj.(wsmsg.IncomingMessage); ok {
var span *trace.Span
childCtx, span = dtracing.StartSpan(childCtx, fmt.Sprintf("Recv. ws/%s", inMsg.GetType()))
span.AddAttributes(trace.StringAttribute("req_id", inMsg.GetReqID()))
defer span.End()
commonIn := inMsg.GetCommon()
dataStr := gjson.GetBytes(rawMsg, "data").Raw
if len(dataStr) > 15000 { // sometimes it is insanely big
dataStr = "[TRUNCATED]" + dataStr[:15000]
}
var currentHeadNum uint64
if headBlock := ws.subscriptionHub.HeadBlock(); headBlock != nil {
currentHeadNum = headBlock.Num()
}
var relToHead uint64
if commonIn.StartBlock <= 0 {
relToHead = uint64(-commonIn.StartBlock)
} else {
relToHead = currentHeadNum - uint64(commonIn.StartBlock)
}
//////////////////////////////////////////////////////////////////////
// Billable event on Websocket inbound
//////////////////////////////////////////////////////////////////////
dmetering.EmitWithCredentials(dmetering.Event{
Source: "eosws",
Kind: "Websocket Message",
Method: inMsg.GetType(),
RequestsCount: 1,
IngressBytes: int64(len(rawMsg)),
}, ws.creds)
//////////////////////////////////////////////////////////////////////
TrackUserEvent(childCtx, "ws_conn_handle_message",
"data", dataStr,
"req_id", commonIn.ReqID,
"msg_type", inMsg.GetType(),
"start_block", commonIn.StartBlock,
"with_progress", commonIn.WithProgress,
"fetch", commonIn.Fetch,
"listen", commonIn.Listen,
"start_rel_to_head", relToHead,
)
childLogger := logging.Logger(childCtx, zlog).With(zap.String("req", inMsg.GetID()))
childCtx = logging.WithLogger(childCtx, childLogger)
}
switch msg := obj.(type) {
case *wsmsg.Unlisten:
err := ws.ShutdownListener(childCtx, msg.Data.ReqID)
if err != nil {
ws.EmitErrorReply(childCtx, msg, derr.Wrap(err, "unable to unlisten listener"))
} else {
ws.EmitReply(childCtx, msg, wsmsg.NewUnlistened())
}
case *wsmsg.GetTransaction:
ws.onGetTransaction(childCtx, msg)
case *wsmsg.GetVoteTally:
ws.onGetVoteTally(childCtx, msg)
case *wsmsg.GetTableRows:
ws.onGetTableRows(childCtx, msg)
case *wsmsg.GetActionTraces:
ws.onGetActionTraces(childCtx, msg)
case *wsmsg.GetPrice:
ws.onGetPrice(childCtx, msg)
case *wsmsg.GetHeadInfo:
ws.onGetHeadInfo(childCtx, msg)
case *wsmsg.GetAccount:
ws.onAccount(childCtx, msg)
}
}
func (ws *WSConn) Emit(ctx context.Context, msg wsmsg.OutgoingMessager) {
zlogger := logging.Logger(ctx, zlog)
msgType, err := wsmsg.GetType(msg)
if err != nil {
zlogger.Error("error getting message type", zap.Error(err))
ws.Shutdown(err)
}
msg.SetType(msgType)
msgBytes, err := json.Marshal(msg)
if err != nil {
zlogger.Error("error marshalling message", zap.Error(err))
ws.Shutdown(err)
}
//////////////////////////////////////////////////////////////////////
// Billable event on Websocket outbound
//////////////////////////////////////////////////////////////////////
dmetering.EmitWithContext(dmetering.Event{
Source: "eosws",
Kind: "Websocket Message",
Method: msgType,
ResponsesCount: 1,
EgressBytes: int64(len(msgBytes)),
}, ctx)
//////////////////////////////////////////////////////////////////////
ws.emitLock.Lock()
_ = ws.conn.SetWriteDeadline(time.Now().Add(1 * time.Minute))
if err := ws.conn.WriteMessage(websocket.TextMessage, msgBytes); err != nil {
zlogger.Info("unable to write message back to client", zap.Error(err))
ws.Shutdown(err)
}
ws.emitLock.Unlock()
}
func (ws *WSConn) EmitReply(ctx context.Context, originatingMsg wsmsg.IncomingMessager, msg wsmsg.OutgoingMessager) {
msg.SetReqID(originatingMsg.GetReqID())
ws.Emit(ctx, msg)
}
func (ws *WSConn) EmitErrorReply(ctx context.Context, msg wsmsg.IncomingMessager, err error) {
ws.EmitError(ctx, msg.GetReqID(), err)
}
func (ws *WSConn) EmitError(ctx context.Context, reqID string, err error) {
if ctx.Err() == context.Canceled {
return
}
response := derr.ToErrorResponse(ctx, err)
if response.Status >= 500 {
ws.emitServerErrorResponse(ctx, reqID, response)
} else {
ws.emitClientErrorResponse(ctx, reqID, response)
}
}
func (ws *WSConn) emitClientErrorResponse(ctx context.Context, reqID string, response *derr.ErrorResponse) {
TrackUserEvent(ctx, "ws_conn_client_error", "error", response)
logging.Logger(ctx, zlog).Info("emiting client error reply", zap.Error(response))
ws.Emit(ctx, wsmsg.NewError(reqID, response))
}
func (ws *WSConn) emitServerErrorResponse(ctx context.Context, reqID string, response *derr.ErrorResponse) {
TrackUserEvent(ctx, "ws_conn_server_error", "error", response)
logging.Logger(ctx, zlog).Error("emiting server error reply", zap.Error(response))
ws.Emit(ctx, wsmsg.NewError(reqID, response))
}