From 309e29c245a01825fc9630103311fd04de99fa5e Mon Sep 17 00:00:00 2001 From: Thane Thomson Date: Tue, 22 Sep 2020 02:47:43 -0400 Subject: [PATCH] rpc: revert JSON-RPC/WebSocket response batching (#5378) Revert the JSON-RPC/WebSocket response serialization format to the standard way (i.e. a single RPC response per WebSocket text message) to avoid breaking clients. Serialization format changes will be discussed in an upcoming ADR. Closes: #5373 --- rpc/jsonrpc/server/ws_handler.go | 35 ++------------------------------ 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/rpc/jsonrpc/server/ws_handler.go b/rpc/jsonrpc/server/ws_handler.go index c04b6057b..7faf76a68 100644 --- a/rpc/jsonrpc/server/ws_handler.go +++ b/rpc/jsonrpc/server/ws_handler.go @@ -28,10 +28,6 @@ const ( defaultWSPingPeriod = (defaultWSReadWait * 9) / 10 ) -var ( - newline = []byte{'\n'} -) - // WebsocketManager provides a WS handler for incoming connections and passes a // map of functions along with any additional params to new connections. // NOTE: The websocket path is defined externally, e.g. in node/node.go @@ -439,40 +435,13 @@ func (wsc *wsConnection) writeRoutine() { return } case msg := <-wsc.writeChan: - if err := wsc.baseConn.SetWriteDeadline(time.Now().Add(wsc.writeWait)); err != nil { - wsc.Logger.Error("Failed to set write deadline", "err", err) - return - } - jsonBytes, err := json.MarshalIndent(msg, "", " ") if err != nil { wsc.Logger.Error("Failed to marshal RPCResponse to JSON", "err", err) continue } - - w, err := wsc.baseConn.NextWriter(websocket.TextMessage) - if err != nil { - wsc.Logger.Error("Can't get NextWriter", "err", err) - return - } - w.Write(jsonBytes) //nolint:errcheck //ignore error - - // Add queued messages to the current websocket message. - n := len(wsc.writeChan) - for i := 0; i < n; i++ { - w.Write(newline) //nolint:errcheck //ignore error - - msg = <-wsc.writeChan - jsonBytes, err = json.MarshalIndent(msg, "", " ") - if err != nil { - wsc.Logger.Error("Failed to marshal RPCResponse to JSON", "err", err) - continue - } - w.Write(jsonBytes) //nolint:errcheck //ignore error - } - - if err := w.Close(); err != nil { - wsc.Logger.Error("Can't close NextWriter", "err", err) + if err = wsc.writeMessageWithDeadline(websocket.TextMessage, jsonBytes); err != nil { + wsc.Logger.Error("Failed to write response", "err", err, "msg", msg) return } }