-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_manager.go
352 lines (289 loc) · 8.81 KB
/
client_manager.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
package sio
import (
"time"
"github.com/karagenc/socket.io-go/internal/sync"
eio "github.com/karagenc/socket.io-go/engine.io"
eioparser "github.com/karagenc/socket.io-go/engine.io/parser"
"github.com/karagenc/socket.io-go/parser"
jsonparser "github.com/karagenc/socket.io-go/parser/json"
"github.com/karagenc/socket.io-go/parser/json/serializer/stdjson"
)
type (
ManagerConfig struct {
// For custom parsers
ParserCreator parser.Creator
// Configuration for the Engine.IO.
EIO eio.ClientConfig
// Should we disallow reconnections?
// Default: false (allow reconnections)
NoReconnection bool
// How many reconnection attempts should we try?
// Default: 0 (Infinite)
ReconnectionAttempts uint32
// The time delay between reconnection attempts.
// Default: 1 second
ReconnectionDelay *time.Duration
// The max time delay between reconnection attempts.
// Default: 5 seconds
ReconnectionDelayMax *time.Duration
// Used in the exponential backoff jitter when reconnecting.
// This value is required to be between 0 and 1
//
// Default: 0.5
RandomizationFactor *float32
// For debugging purposes. Leave it nil if it is of no use.
//
// This only applies to Socket.IO. For Engine.IO, use EIO.Debugger.
Debugger Debugger
}
Manager struct {
url string
eioConfig eio.ClientConfig
debug Debugger
// Also a reconnectMu
connectMu sync.Mutex
state clientConnectionState
stateMu sync.RWMutex
eio eio.ClientSocket
eioPacketQueue *packetQueue
eioMu sync.RWMutex
// This mutex is used for protecting parser from concurrent calls.
// Due to the modular and concurrent nature of Engine.IO,
// we should use a mutex to ensure that the Engine.IO doesn't access
// the parser's Add method from multiple goroutines.
parserMu sync.Mutex
parser parser.Parser
noReconnection bool
reconnectionAttempts uint32
reconnectionDelay time.Duration
reconnectionDelayMax time.Duration
randomizationFactor float32
sockets *clientSocketStore
// For testing purposes
onNewSocket func(socket *clientSocket)
backoff *backoff
skipReconnect bool
skipReconnectMu sync.RWMutex
openHandlers *handlerStore[*ManagerOpenFunc]
pingHandlers *handlerStore[*ManagerPingFunc]
errorHandlers *handlerStore[*ManagerErrorFunc]
closeHandlers *handlerStore[*ManagerCloseFunc]
reconnectHandlers *handlerStore[*ManagerReconnectFunc]
reconnectAttemptHandlers *handlerStore[*ManagerReconnectAttemptFunc]
reconnectErrorHandlers *handlerStore[*ManagerReconnectErrorFunc]
reconnectFailedHandlers *handlerStore[*ManagerReconnectFailedFunc]
// Callbacks to destroy subs
subs []func()
subsMu sync.Mutex
}
)
const (
DefaultReconnectionDelay = 1 * time.Second
DefaultReconnectionDelayMax = 5 * time.Second
DefaultRandomizationFactor float32 = 0.5
)
// This function creates a new Manager for client sockets.
//
// You should create a new Socket using the Socket
// method of the Manager returned by this function.
// If you don't do that, server will terminate your connection. See: https://socket.io/docs/v4/server-initialization/#connectTimeout
func NewManager(url string, config *ManagerConfig) *Manager {
if config == nil {
config = new(ManagerConfig)
}
io := &Manager{
url: url,
eioConfig: config.EIO,
eioPacketQueue: newPacketQueue(),
noReconnection: config.NoReconnection,
reconnectionAttempts: config.ReconnectionAttempts,
sockets: newClientSocketStore(),
onNewSocket: func(socket *clientSocket) {}, // Noop by default.
openHandlers: newHandlerStore[*ManagerOpenFunc](),
pingHandlers: newHandlerStore[*ManagerPingFunc](),
errorHandlers: newHandlerStore[*ManagerErrorFunc](),
closeHandlers: newHandlerStore[*ManagerCloseFunc](),
reconnectHandlers: newHandlerStore[*ManagerReconnectFunc](),
reconnectAttemptHandlers: newHandlerStore[*ManagerReconnectAttemptFunc](),
reconnectErrorHandlers: newHandlerStore[*ManagerReconnectErrorFunc](),
reconnectFailedHandlers: newHandlerStore[*ManagerReconnectFailedFunc](),
}
if config.Debugger != nil {
io.debug = config.Debugger
} else {
io.debug = newNoopDebugger()
}
io.debug = io.debug.WithContext("[sio/client] Manager with URL: " + truncateURL(url))
if config.ReconnectionDelay != nil {
io.reconnectionDelay = *config.ReconnectionDelay
} else {
io.reconnectionDelay = DefaultReconnectionDelay
}
if config.ReconnectionDelayMax != nil {
io.reconnectionDelayMax = *config.ReconnectionDelayMax
} else {
io.reconnectionDelayMax = DefaultReconnectionDelayMax
}
if config.RandomizationFactor != nil {
io.randomizationFactor = *config.RandomizationFactor
} else {
io.randomizationFactor = DefaultRandomizationFactor
}
io.backoff = newBackoff(io.reconnectionDelay, io.reconnectionDelayMax, io.randomizationFactor)
parserCreator := config.ParserCreator
if parserCreator == nil {
json := stdjson.New()
parserCreator = jsonparser.NewCreator(0, json)
}
io.parser = parserCreator()
return io
}
func (m *Manager) Socket(namespace string, config *ClientSocketConfig) ClientSocket {
socket := m.socket(namespace, config)
m.onNewSocket(socket)
return socket
}
func (m *Manager) socket(namespace string, config *ClientSocketConfig) *clientSocket {
if namespace == "" {
namespace = "/"
} else if len(namespace) > 0 && namespace[0] != '/' {
namespace = "/" + namespace
}
if config == nil {
config = new(ClientSocketConfig)
} else {
// User can modify the config. We copy the config here in order to avoid problems.
configCopy := *config
config = &configCopy
}
socket, ok := m.sockets.get(namespace)
if !ok {
socket = newClientSocket(config, m, namespace, m.parser)
m.sockets.set(socket)
}
return socket
}
func (m *Manager) onEIOPacket(packets ...*eioparser.Packet) {
m.parserMu.Lock()
defer m.parserMu.Unlock()
for _, packet := range packets {
switch packet.Type {
case eioparser.PacketTypeMessage:
err := m.parser.Add(packet.Data, m.onParserFinish)
if err != nil {
go m.onClose(ReasonParseError, err)
return
}
case eioparser.PacketTypePing:
m.pingHandlers.forEach(func(handler *ManagerPingFunc) { (*handler)() }, true)
}
}
}
func (m *Manager) onParserFinish(header *parser.PacketHeader, eventName string, decode parser.Decode) {
if header.Namespace == "" {
header.Namespace = "/"
}
socket, ok := m.sockets.get(header.Namespace)
if !ok {
return
}
go socket.onPacket(header, eventName, decode)
}
func (m *Manager) packet(packets ...*eioparser.Packet) {
m.eioMu.RLock()
defer m.eioMu.RUnlock()
m.eioPacketQueue.add(packets...)
}
func (m *Manager) onError(err error) {
m.errorHandlers.forEach(func(handler *ManagerErrorFunc) { (*handler)(err) }, true)
}
func (m *Manager) Open() {
go m.open()
}
func (m *Manager) open() {
m.debug.Log("Opening")
err := m.connect(false)
if err != nil {
m.cleanup()
m.maybeReconnectOnOpen()
}
}
func (m *Manager) maybeReconnectOnOpen() {
reconnect := m.backoff.attempts() == 0 && !m.noReconnection
if reconnect {
m.reconnect(false)
}
}
func (m *Manager) onReconnect() {
attempts := m.backoff.attempts()
m.backoff.reset()
m.reconnectHandlers.forEach(func(handler *ManagerReconnectFunc) { (*handler)(attempts) }, true)
}
func (m *Manager) closePacketQueue(pq *packetQueue) {
go func() {
pq.waitForDrain(2 * time.Minute)
pq.close()
}()
}
func (m *Manager) destroy(_ *clientSocket) {
for _, socket := range m.sockets.getAll() {
if socket.Active() {
m.debug.Log("Socket (nsp: `" + socket.namespace + "`) is still active, skipping close")
return
}
}
m.Close()
}
func (m *Manager) cleanup() {
m.subsMu.Lock()
subs := m.subs
m.subs = nil
m.subsMu.Unlock()
for _, sub := range subs {
sub()
}
m.resetParser()
}
func (m *Manager) onClose(reason Reason, err error) {
m.debug.Log("Closed. Reason", reason)
m.cleanup()
m.backoff.reset()
m.stateMu.Lock()
m.state = clientConnStateDisconnected
m.stateMu.Unlock()
m.closeHandlers.forEach(func(handler *ManagerCloseFunc) { (*handler)(reason, err) }, true)
m.skipReconnectMu.RLock()
skipReconnect := m.skipReconnect
m.skipReconnectMu.RUnlock()
if !m.noReconnection && !skipReconnect {
go m.reconnect(false)
}
}
func (m *Manager) Close() {
m.debug.Log("Disconnecting")
m.stateMu.Lock()
m.state = clientConnStateDisconnected
m.stateMu.Unlock()
m.skipReconnectMu.Lock()
m.skipReconnect = true
m.skipReconnectMu.Unlock()
m.onClose(ReasonForcedClose, nil)
m.eioMu.RLock()
defer m.eioMu.RUnlock()
eio := m.eio
if eio != nil {
go eio.Close()
}
m.closePacketQueue(m.eioPacketQueue)
}
func (m *Manager) resetParser() {
m.parserMu.Lock()
defer m.parserMu.Unlock()
m.parser.Reset()
}
func truncateURL(url string) string {
if len(url) > 50 {
return url[:50] + "..."
}
return url
}