forked from iotaledger/hive.go
/
client.go
349 lines (279 loc) · 8.1 KB
/
client.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
package websockethub
import (
"context"
"fmt"
"sync"
"time"
"go.uber.org/atomic"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
"github.com/izuc/zipp.foundation/core/logger"
"github.com/izuc/zipp.foundation/core/timeutil"
)
const (
// time allowed to write a message to the peer.
writeWait = 10 * time.Second
// time allowed to read the next pong message from the peer.
pongWait = 5 * time.Second
// send pings to peer with this period.
pingPeriod = 30 * time.Second
)
// WebsocketMsg is a message received via websocket.
type WebsocketMsg struct {
// MsgType is the type of the message based on RFC 6455.
MsgType websocket.MessageType
// Data is the received data of the message.
Data []byte
}
// ClientID is the ID of a client.
type ClientID uint32
// Client is a middleman between the node and the websocket connection.
type Client struct {
*logger.WrappedLogger
// the id of the client.
id ClientID
// the websocket hub the client is connected to.
hub *Hub
// the websocket connection.
conn *websocket.Conn
// a context which is canceled when the ping times out and the client should be dropped.
ctx context.Context
cancel context.CancelFunc
// a channel which is closed when the websocket client is disconnected.
ExitSignal chan struct{}
// buffered channel of outbound messages.
sendChan chan interface{}
// a channel which is closed when the writePump of the client exited.
// this is used signal the hub to not send messages to sendChan anymore.
sendChanClosed chan struct{}
// channel of inbound messages.
// this will be created by the user if receiving messages is needed.
ReceiveChan chan *WebsocketMsg
// onConnect gets called when the client was registered
onConnect func(*Client)
// onDisconnect gets called when the client was disconnected
onDisconnect func(*Client)
// FilterCallback is used to filter messages to clients on BroadcastMsg
FilterCallback func(c *Client, data interface{}) bool
// startWaitGroup is used to synchronize the start of the writePump and receivePong func
startWaitGroup sync.WaitGroup
// shutdownWaitGroup is used wait until writePump and receivePong func stopped
shutdownWaitGroup sync.WaitGroup
// indicates that the client was shut down
shutdownFlag *atomic.Bool
// indicates the max amount of bytes that will be read from a client, i.e. the max message size
readLimit int64
}
func NewClient(hub *Hub, conn *websocket.Conn, onConnect func(client *Client), onDisconnect func(client *Client)) *Client {
ctx, cancel := context.WithCancel(hub.ctx)
clientID := ClientID(hub.lastClientID.Inc())
return &Client{
WrappedLogger: logger.NewWrappedLogger(hub.logger.Named(fmt.Sprintf("client %d", clientID))),
id: clientID,
hub: hub,
conn: conn,
ctx: ctx,
cancel: cancel,
ExitSignal: make(chan struct{}),
sendChan: make(chan interface{}, hub.clientSendChannelSize),
sendChanClosed: make(chan struct{}),
onConnect: onConnect,
onDisconnect: onDisconnect,
shutdownFlag: atomic.NewBool(false),
readLimit: hub.clientReadLimit,
}
}
// ID returns the id of the client.
func (c *Client) ID() ClientID {
return c.id
}
// keepAlive sends ping messages to the client and waits for pong responses.
// if no pong response is received in time, the client context is canceled.
func (c *Client) keepAlive() {
// start the timer with 0, so it fires immediately
pingTimer := time.NewTimer(0)
defer func() {
timeutil.CleanupTimer(pingTimer)
// always cancel the client context if we exit this function to clean up the client
c.cancel()
c.shutdownWaitGroup.Done()
}()
sendPing := func() error {
pongCtx, pongCancel := context.WithTimeout(c.ctx, pongWait)
defer pongCancel()
if err := c.conn.Ping(pongCtx); err != nil {
return err
}
// reset the ping timer, so a new ping event is fired after "pingPeriod".
// we can safely reset the timer here, because the timer channel was consumed already.
pingTimer.Reset(pingPeriod)
return nil
}
c.startWaitGroup.Done()
for {
select {
case <-c.ctx.Done():
// the client context is done
return
case <-c.ExitSignal:
// the Hub closed the channel
return
case <-pingTimer.C:
if err := sendPing(); err != nil {
// failed to send ping or receive pong
// => client seems to be unhealthy
c.LogWarn(err.Error())
// send an unregister message to the hub
_ = c.hub.Unregister(c)
return
}
}
}
}
// readPump reads incoming messages and stops if the client does not respond to the keep alive pings
// that are sent periodically in the keepAlive function.
//
// at most one reader per websocket connection is allowed.
func (c *Client) readPump() {
defer func() {
select {
case <-c.ctx.Done():
// the client context is done
case <-c.ExitSignal:
// the Hub closed the channel
default:
// send an unregister message to the hub
_ = c.hub.Unregister(c)
}
if c.ReceiveChan != nil {
// drain and close the receive channel
drainLoop:
for {
select {
case <-c.ReceiveChan:
default:
break drainLoop
}
}
close(c.ReceiveChan)
}
c.shutdownWaitGroup.Done()
}()
c.startWaitGroup.Done()
c.conn.SetReadLimit(c.readLimit)
for {
msgType, data, err := c.conn.Read(c.ctx)
if err != nil {
if websocket.CloseStatus(err) == websocket.StatusGoingAway || websocket.CloseStatus(err) == websocket.StatusAbnormalClosure {
c.LogWarnf("Websocket ReadMessage error: %v", err)
}
return
}
if c.ReceiveChan != nil {
select {
case <-c.ctx.Done():
// the client context is done
return
case <-c.ExitSignal:
// the Hub closed the channel
return
case c.ReceiveChan <- &WebsocketMsg{MsgType: msgType, Data: data}:
// send the received message to the user
}
}
}
}
// writePump pumps messages from the node to the websocket connection.
//
// at most one writer per websocket connection is allowed.
func (c *Client) writePump() {
defer func() {
// signal the hub to not send messages to sendChan anymore
close(c.sendChanClosed)
// mark the client as shutdown
c.shutdownFlag.Store(true)
select {
case <-c.ctx.Done():
// the client context is done
case <-c.ExitSignal:
// the Hub closed the channel
default:
// send an unregister message to the hub
_ = c.hub.Unregister(c)
}
// close the websocket connection
if err := c.conn.Close(websocket.StatusNormalClosure, ""); err != nil {
c.LogWarnf("Websocket closing error: %v", err)
}
c.shutdownWaitGroup.Done()
}()
sendMsg := func(msg any) error {
ctx, cancel := context.WithTimeout(c.ctx, writeWait)
defer cancel()
return wsjson.Write(ctx, c.conn, msg)
}
for {
select {
case <-c.ctx.Done():
// the client context is done
return
case <-c.ExitSignal:
// the Hub closed the channel
if err := c.conn.Close(websocket.StatusNormalClosure, ""); err != nil {
c.LogWarnf("Websocket closing error: %v", err)
}
return
case msg, ok := <-c.sendChan:
if !ok {
// the Hub closed the channel
if err := c.conn.Close(websocket.StatusNormalClosure, ""); err != nil {
c.LogWarnf("Websocket closing error: %v", err)
}
return
}
if err := sendMsg(msg); err != nil {
c.LogWarnf("Websocket error: %v", err)
return
}
}
}
}
// Send sends a message to the client.
func (c *Client) Send(ctx context.Context, msg interface{}, dontDrop ...bool) error {
if c.hub.Stopped() {
// hub was already shut down
return ErrWebsocketServerUnavailable
}
if c.shutdownFlag.Load() {
// client was already shutdown
return ErrClientDisconnected
}
if len(dontDrop) > 0 && dontDrop[0] {
select {
case <-c.ctx.Done():
return c.ctx.Err()
case <-ctx.Done():
return ctx.Err()
case <-c.ExitSignal:
return ErrClientDisconnected
case <-c.sendChanClosed:
return ErrClientDisconnected
case c.sendChan <- msg:
return nil
}
}
select {
case <-c.ctx.Done():
return c.ctx.Err()
case <-ctx.Done():
return ctx.Err()
case <-c.ExitSignal:
return ErrClientDisconnected
case <-c.sendChanClosed:
return ErrClientDisconnected
case c.sendChan <- msg:
return nil
default:
return nil
}
}