-
Notifications
You must be signed in to change notification settings - Fork 3
/
websocket.go
305 lines (269 loc) · 7.76 KB
/
websocket.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
// Copyright (c) 2018, The Decred developers
// Copyright (c) 2017, The dcrdata developers
// See LICENSE for details.
package explorer
import (
"sync"
"time"
)
const (
wsWriteTimeout = 10 * time.Second
wsReadTimeout = 12 * time.Second
pingInterval = 8 * time.Second
tickerSigReset int = iota
tickerSigStop
bufferSend
bufferTickerInterval = 5
newTxBufferSize = 5
clientSignalSize = 5
sigNewBlock hubSignal = iota
sigMempoolUpdate
sigPingAndUserCount
sigNewTx
)
// WebSocketMessage represents the JSON object used to send and received typed
// messages to the web client.
type WebSocketMessage struct {
EventId string `json:"event"`
Message string `json:"message"`
}
// Event type field for an SSE event
var eventIDs = map[hubSignal]string{
sigNewBlock: "newblock",
sigMempoolUpdate: "mempool",
sigPingAndUserCount: "ping",
sigNewTx: "newtx",
}
// WebsocketHub and its event loop manage all websocket client connections.
// WebsocketHub is responsible for closing all connections registered with it.
// If the event loop is running, calling (*WebsocketHub).Stop() will handle it.
type WebsocketHub struct {
clients map[*hubSpoke]*client
Register chan *clientHubSpoke
Unregister chan *hubSpoke
HubRelay chan hubSignal
NewTxChan chan *MempoolTx
newTxBuffer []*MempoolTx
bufferMtx *sync.Mutex
bufferTickerChan chan int
sendBufferChan chan int
quitWSHandler chan struct{}
}
type client struct {
sync.RWMutex
newTxs []*MempoolTx
}
type hubSignal int
type hubSpoke chan hubSignal
// NewWebsocketHub creates a new WebsocketHub
func NewWebsocketHub() *WebsocketHub {
return &WebsocketHub{
clients: make(map[*hubSpoke]*client),
Register: make(chan *clientHubSpoke),
Unregister: make(chan *hubSpoke),
HubRelay: make(chan hubSignal),
NewTxChan: make(chan *MempoolTx),
newTxBuffer: make([]*MempoolTx, 0, newTxBufferSize),
bufferTickerChan: make(chan int, clientSignalSize),
bufferMtx: new(sync.Mutex),
sendBufferChan: make(chan int, clientSignalSize),
quitWSHandler: make(chan struct{}),
}
}
type clientHubSpoke struct {
cl *client
c *hubSpoke
}
// NumClients returns the number of clients connected to the websocket hub
func (wsh *WebsocketHub) NumClients() int {
return len(wsh.clients)
}
// RegisterClient registers a websocket connection with the hub, and returns a
// pointer to the new client data object.
func (wsh *WebsocketHub) RegisterClient(c *hubSpoke) *client {
cl := new(client)
wsh.Register <- &clientHubSpoke{cl, c}
return cl
}
// registerClient should only be called from the run loop
func (wsh *WebsocketHub) registerClient(ch *clientHubSpoke) {
wsh.clients[ch.c] = ch.cl
log.Debugf("Registered new websocket client (%d).", wsh.NumClients())
}
// UnregisterClient unregisters the input websocket connection via the main
// run() loop. This call will block if the run() loop is not running.
func (wsh *WebsocketHub) UnregisterClient(c *hubSpoke) {
wsh.Unregister <- c
}
// unregisterClient should only be called from the loop in run().
func (wsh *WebsocketHub) unregisterClient(c *hubSpoke) {
if _, ok := wsh.clients[c]; !ok {
// unknown client, do not close channel
log.Warnf("unknown client")
return
}
delete(wsh.clients, c)
// Close the channel, but make sure the client didn't do it
safeClose(*c)
}
// Periodically ping clients over websocket connection. Stop the ping loop by
// closing the returned channel.
func (wsh *WebsocketHub) pingClients() chan<- struct{} {
stopPing := make(chan struct{})
go func() {
// start the client ping ticker
ticker := time.NewTicker(pingInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
wsh.HubRelay <- sigPingAndUserCount
case _, ok := <-stopPing:
if !ok {
log.Errorf("Do not send on stopPing channel, only close it.")
}
return
}
}
}()
return stopPing
}
func safeClose(cc hubSpoke) {
select {
case _, ok := <-cc:
if !ok {
log.Debug("Channel already closed!")
return
}
default:
}
close(cc)
}
// Stop kills the run() loop and unregisteres all clients (connections).
func (wsh *WebsocketHub) Stop() {
// end the run() loop, allowing in progress operations to complete
wsh.quitWSHandler <- struct{}{}
}
func (wsh *WebsocketHub) run() {
log.Info("Starting WebsocketHub run loop.")
// start the buffer send ticker loop
go wsh.periodicBufferSend()
// start the client ping ticker
stopPing := wsh.pingClients()
defer close(stopPing)
for {
events:
select {
case hubSignal := <-wsh.HubRelay:
var newtx *MempoolTx
switch hubSignal {
case sigNewBlock:
log.Infof("Signaling new block to %d websocket clients.", len(wsh.clients))
case sigPingAndUserCount:
log.Tracef("Signaling ping/user count to %d websocket clients.", len(wsh.clients))
case sigMempoolUpdate:
if len(wsh.clients) > 0 {
log.Infof("Signaling mempool update to %d websocket clients.", len(wsh.clients))
}
case sigNewTx:
newtx = <-wsh.NewTxChan
log.Tracef("Received new tx %s", newtx.Hash)
wsh.MaybeSendTxns(newtx)
default:
log.Errorf("Unknown hub signal: %v", hubSignal)
break events
}
for client := range wsh.clients {
// Don't signal the client on new tx, another case handles that
if hubSignal == sigNewTx {
break
}
// signal or unregister the client
select {
case *client <- hubSignal:
default:
wsh.unregisterClient(client)
}
}
case ch := <-wsh.Register:
wsh.registerClient(ch)
case c := <-wsh.Unregister:
wsh.unregisterClient(c)
case _, ok := <-wsh.quitWSHandler:
if !ok {
log.Error("close channel already closed. This should not happen.")
return
}
close(wsh.quitWSHandler)
// end the buffer interval send loop
wsh.bufferTickerChan <- tickerSigStop
// unregister all clients
for client := range wsh.clients {
wsh.unregisterClient(client)
}
return
case <-wsh.sendBufferChan:
wsh.bufferMtx.Lock()
if len(wsh.newTxBuffer) == 0 {
wsh.bufferMtx.Unlock()
continue
}
txs := make([]*MempoolTx, len(wsh.newTxBuffer))
copy(txs, wsh.newTxBuffer)
wsh.newTxBuffer = make([]*MempoolTx, 0, newTxBufferSize)
wsh.bufferMtx.Unlock()
if len(wsh.clients) > 0 {
log.Debugf("Signaling %d new tx to %d clients", len(txs), len(wsh.clients))
}
for signal, client := range wsh.clients {
client.Lock()
client.newTxs = txs
client.Unlock()
select {
case *signal <- sigNewTx:
default:
wsh.unregisterClient(signal)
}
}
}
}
}
// MaybeSendTxns adds a mempool transaction to the client broadcast buffer. If
// the buffer is at capacity, a goroutine is launched to signal for the
// transactions to be sent to the clients.
func (wsh *WebsocketHub) MaybeSendTxns(tx *MempoolTx) {
if wsh.addTxToBuffer(tx) {
// This is called from the event loop, so these sends channel may not be
// blocking.
go func() {
wsh.bufferTickerChan <- tickerSigReset
wsh.sendBufferChan <- bufferSend
}()
}
}
// addTxToBuffer adds a tx to the buffer, then returns if the buffer is full
func (wsh *WebsocketHub) addTxToBuffer(tx *MempoolTx) bool {
wsh.bufferMtx.Lock()
defer wsh.bufferMtx.Unlock()
wsh.newTxBuffer = append(wsh.newTxBuffer, tx)
return len(wsh.newTxBuffer) >= newTxBufferSize
}
// periodicBufferSend initiates a buffer send every bufferTickerInterval seconds
func (wsh *WebsocketHub) periodicBufferSend() {
ticker := time.NewTicker(bufferTickerInterval * time.Second)
for {
select {
case <-ticker.C:
wsh.sendBufferChan <- bufferSend
case sig := <-wsh.bufferTickerChan:
switch sig {
case tickerSigReset:
ticker.Stop()
ticker = time.NewTicker(bufferTickerInterval * time.Second)
case tickerSigStop:
close(wsh.bufferTickerChan)
return
}
}
}
}