This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
/
wsclient.go
372 lines (322 loc) · 11.7 KB
/
wsclient.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
package websocket
import (
"container/list"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"github.com/bytom/bytom/errors"
)
// websocketSendBufferSize is the number of elements the send channel
// can queue before blocking. Note that this only applies to requests
// handled directly in the websocket client input handler or the async
// handler since notifications have their own queuing mechanism
// independent of the send channel buffer.
const (
logModule = "websocket"
websocketSendBufferSize = 50
)
var (
// ErrWSParse means a request parsing error
ErrWSParse = errors.New("Websocket request parsing error")
// ErrWSInternal means service handling errors
ErrWSInternal = errors.New("Websocket Internal error")
// ErrWSClientQuit means the websocket client is disconnected
ErrWSClientQuit = errors.New("Websocket client quit")
// timeZeroVal is simply the zero value for a time.Time and is used to avoid creating multiple instances.
timeZeroVal time.Time
)
type semaphore chan struct{}
func makeSemaphore(n int) semaphore {
return make(chan struct{}, n)
}
func (s semaphore) acquire() { s <- struct{}{} }
func (s semaphore) release() { <-s }
// wsTopicHandler describes a callback function used to handle a specific topic.
type wsTopicHandler func(*WSClient)
// wsHandlers maps websocket topic strings to appropriate websocket handler
// functions. This is set by init because help references wsHandlers and thus
// causes a dependency loop.
var wsHandlers = map[string]wsTopicHandler{
"notify_raw_blocks": handleNotifyBlocks,
"notify_new_transactions": handleNotifyNewTransactions,
"stop_notify_raw_blocks": handleStopNotifyBlocks,
"stop_notify_new_transactions": handleStopNotifyNewTransactions,
}
// responseMessage houses a message to send to a connected websocket client as
// well as a channel to reply on when the message is sent.
type responseMessage struct {
msg []byte
doneChan chan bool
}
// WSClient provides an abstraction for handling a websocket client. The
// overall data flow is split into 3 main goroutines, a possible 4th goroutine
// for long-running operations (only started if request is made), and a
// websocket manager which is used to allow things such as broadcasting
// requested notifications to all connected websocket clients. Inbound
// messages are read via the inHandler goroutine and generally dispatched to
// their own handler. However, certain potentially long-running operations such
// as rescans, are sent to the asyncHander goroutine and are limited to one at a
// time. There are two outbound message types - one for responding to client
// requests and another for async notifications. Responses to client requests
// use SendMessage which employs a buffered channel thereby limiting the number
// of outstanding requests that can be made. Notifications are sent via
// QueueNotification which implements a queue via notificationQueueHandler to
// ensure sending notifications from other subsystems can't block. Ultimately,
// all messages are sent via the outHandler.
type WSClient struct {
sync.Mutex
conn *websocket.Conn
// disconnected indicated whether or not the websocket client is disconnected.
disconnected bool
// addr is the remote address of the client.
addr string
serviceRequestSem semaphore
ntfnChan chan []byte
sendChan chan responseMessage
quit chan struct{}
wg sync.WaitGroup
notificationMgr *WSNotificationManager
}
// NewWebsocketClient means to create a new object to the connected websocket client
func NewWebsocketClient(w http.ResponseWriter, r *http.Request, notificationMgr *WSNotificationManager) (*WSClient, error) {
// Limit max number of websocket clients.
if notificationMgr.IsMaxConnect() {
return nil, fmt.Errorf("numOfMaxWS: %d, disconnecting: %s", notificationMgr.MaxNumWebsockets, r.RemoteAddr)
}
// Attempt to upgrade the connection to a websocket connection using the default size for read/write buffers.
conn, err := websocket.Upgrade(w, r, nil, 0, 0)
if err != nil {
return nil, err
}
conn.SetReadDeadline(timeZeroVal)
client := &WSClient{
conn: conn,
addr: r.RemoteAddr,
serviceRequestSem: makeSemaphore(notificationMgr.maxNumConcurrentReqs),
ntfnChan: make(chan []byte, 1), // nonblocking sync
sendChan: make(chan responseMessage, websocketSendBufferSize),
quit: make(chan struct{}),
notificationMgr: notificationMgr,
}
return client, nil
}
// inHandler handles all incoming messages for the websocket connection.
func (c *WSClient) inHandler() {
out:
for {
// Break out of the loop once the quit channel has been closed.
// Use a non-blocking select here so we fall through otherwise.
select {
case <-c.quit:
break out
default:
}
_, msg, err := c.conn.ReadMessage()
if err != nil {
if err != io.EOF {
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr, "error": err}).Error("Websocket receive error")
}
break out
}
var request WSRequest
if err = json.Unmarshal(msg, &request); err != nil {
respError := errors.Wrap(err, ErrWSParse)
resp := NewWSResponse(NTRequestStatus.String(), nil, respError)
reply, err := json.Marshal(resp)
if err != nil {
log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Failed to marshal parse failure reply")
continue
}
c.SendMessage(reply, nil)
continue
}
c.serviceRequestSem.acquire()
go func() {
c.serviceRequest(request.Topic)
c.serviceRequestSem.release()
}()
}
// Ensure the connection is closed.
c.Disconnect()
c.wg.Done()
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr}).Debug("Websocket client input handler done")
}
func (c *WSClient) serviceRequest(topic string) {
var respErr error
if wsHandler, ok := wsHandlers[topic]; ok {
wsHandler(c)
} else {
err := fmt.Errorf("There is not this topic: %s", topic)
respErr = errors.Wrap(err, ErrWSInternal)
log.WithFields(log.Fields{"module": logModule, "topic": topic}).Debug("There is not this topic")
}
resp := NewWSResponse(NTRequestStatus.String(), nil, respErr)
reply, err := json.Marshal(resp)
if err != nil {
log.WithFields(log.Fields{"module": logModule, "error": err}).Debug("Failed to marshal parse failure reply")
return
}
c.SendMessage(reply, nil)
}
// notificationQueueHandler handles the queuing of outgoing notifications for the websocket client.
func (c *WSClient) notificationQueueHandler() {
ntfnSentChan := make(chan bool, 1) // nonblocking sync
// pendingNtfns is used as a queue for notifications that are ready to
// be sent once there are no outstanding notifications currently being
// sent.
pendingNtfns := list.New()
waiting := false
out:
for {
select {
// This channel is notified when a message is being queued to
// be sent across the network socket. It will either send the
// message immediately if a send is not already in progress, or
// queue the message to be sent once the other pending messages
// are sent.
case msg := <-c.ntfnChan:
if !waiting {
c.SendMessage(msg, ntfnSentChan)
} else {
pendingNtfns.PushBack(msg)
}
waiting = true
// This channel is notified when a notification has been sent across the network socket.
case <-ntfnSentChan:
// No longer waiting if there are no more messages in the pending messages queue.
next := pendingNtfns.Front()
if next == nil {
waiting = false
continue
}
// Notify the outHandler about the next item to asynchronously send.
msg := pendingNtfns.Remove(next).([]byte)
c.SendMessage(msg, ntfnSentChan)
case <-c.quit:
break out
}
}
// Drain any wait channels before exiting so nothing is left waiting around to send.
cleanup:
for {
select {
case <-c.ntfnChan:
case <-ntfnSentChan:
default:
break cleanup
}
}
c.wg.Done()
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr}).Debug("Websocket client notification queue handler done")
}
// outHandler handles all outgoing messages for the websocket connection.
func (c *WSClient) outHandler() {
out:
for {
// Send any messages ready for send until the quit channel is closed.
select {
case r := <-c.sendChan:
if err := c.conn.WriteMessage(websocket.TextMessage, r.msg); err != nil {
log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Failed to send message to wesocket client")
c.Disconnect()
break out
}
if r.doneChan != nil {
r.doneChan <- true
}
case <-c.quit:
break out
}
}
// Drain any wait channels before exiting so nothing is left waiting around to send.
cleanup:
for {
select {
case r := <-c.sendChan:
if r.doneChan != nil {
r.doneChan <- false
}
default:
break cleanup
}
}
c.wg.Done()
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr}).Debug("Websocket client output handler done")
}
// SendMessage sends the passed json to the websocket client. It is backed
// by a buffered channel, so it will not block until the send channel is full.
// Note however that QueueNotification must be used for sending async
// notifications instead of the this function. This approach allows a limit to
// the number of outstanding requests a client can make without preventing or
// blocking on async notifications.
func (c *WSClient) SendMessage(marshalledJSON []byte, doneChan chan bool) {
// Don't send the message if disconnected.
if c.Disconnected() {
if doneChan != nil {
doneChan <- false
}
return
}
c.sendChan <- responseMessage{msg: marshalledJSON, doneChan: doneChan}
}
// QueueNotification queues the passed notification to be sent to the websocket client.
func (c *WSClient) QueueNotification(marshalledJSON []byte) error {
// Don't queue the message if disconnected.
if c.Disconnected() {
return ErrWSClientQuit
}
c.ntfnChan <- marshalledJSON
return nil
}
// Disconnected returns whether or not the websocket client is disconnected.
func (c *WSClient) Disconnected() bool {
c.Lock()
defer c.Unlock()
return c.disconnected
}
// Disconnect disconnects the websocket client.
func (c *WSClient) Disconnect() {
c.Lock()
defer c.Unlock()
// Nothing to do if already disconnected.
if c.disconnected {
return
}
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr}).Info("Disconnecting websocket client")
close(c.quit)
c.conn.Close()
c.disconnected = true
}
// Start begins processing input and output messages.
func (c *WSClient) Start() {
log.WithFields(log.Fields{"module": logModule, "remoteAddress": c.addr}).Info("Starting websocket client")
c.wg.Add(3)
go c.inHandler()
go c.notificationQueueHandler()
go c.outHandler()
}
// WaitForShutdown blocks until the websocket client goroutines are stopped and the connection is closed.
func (c *WSClient) WaitForShutdown() {
c.wg.Wait()
}
// handleNotifyBlocks implements the notifyblocks topic extension for websocket connections.
func handleNotifyBlocks(wsc *WSClient) {
wsc.notificationMgr.RegisterBlockUpdates(wsc)
}
// handleStopNotifyBlocks implements the stopnotifyblocks topic extension for websocket connections.
func handleStopNotifyBlocks(wsc *WSClient) {
wsc.notificationMgr.UnregisterBlockUpdates(wsc)
}
// handleNotifyNewTransations implements the notifynewtransactions topic extension for websocket connections.
func handleNotifyNewTransactions(wsc *WSClient) {
wsc.notificationMgr.RegisterNewMempoolTxsUpdates(wsc)
}
// handleStopNotifyNewTransations implements the stopnotifynewtransactions topic extension for websocket connections.
func handleStopNotifyNewTransactions(wsc *WSClient) {
wsc.notificationMgr.UnregisterNewMempoolTxsUpdates(wsc)
}