-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_manager_conn.go
204 lines (178 loc) · 4.77 KB
/
client_manager_conn.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
package sio
import (
"math"
"time"
eio "github.com/karagenc/socket.io-go/engine.io"
"github.com/karagenc/socket.io-go/engine.io/parser"
"github.com/karagenc/socket.io-go/internal/sync"
)
// Manager methods that are directly related to
// connection, reconnection, and disconnection functionalities.
type clientConnectionState int
const (
clientConnStateConnecting clientConnectionState = iota
clientConnStateConnected
clientConnStateReconnecting
clientConnStateDisconnected
)
func (m *Manager) connected() bool {
m.stateMu.RLock()
defer m.stateMu.RUnlock()
return m.state == clientConnStateConnected
}
func (m *Manager) connect(recursed bool) (err error) {
// recursed = Is this the first time we're running the connect method?
// In other words: are we recursing?
if !recursed {
m.connectMu.Lock()
defer m.connectMu.Unlock()
m.skipReconnectMu.Lock()
m.skipReconnect = false
m.skipReconnectMu.Unlock()
}
m.stateMu.Lock()
if m.state == clientConnStateConnected {
m.stateMu.Unlock()
return nil
}
m.state = clientConnStateConnecting
m.stateMu.Unlock()
m.eioMu.Lock()
defer m.eioMu.Unlock()
var (
active = true
activeMu sync.Mutex
)
callbacks := eio.Callbacks{
OnPacket: func(packets ...*parser.Packet) {
activeMu.Lock()
if !active {
activeMu.Unlock()
return
}
activeMu.Unlock()
m.onEIOPacket(packets...)
},
OnError: func(err error) {
activeMu.Lock()
if !active {
activeMu.Unlock()
return
}
activeMu.Unlock()
m.onError(err)
},
OnClose: func(reason eio.Reason, err error) {
activeMu.Lock()
if !active {
activeMu.Unlock()
return
}
activeMu.Unlock()
m.onClose(reason, err)
},
}
_eio, err := eio.Dial(m.url, &callbacks, &m.eioConfig)
if err != nil {
m.resetParser()
m.stateMu.Lock()
m.state = clientConnStateDisconnected
m.stateMu.Unlock()
m.errorHandlers.forEach(func(handler *ManagerErrorFunc) { (*handler)(err) }, true)
return err
}
m.stateMu.Lock()
m.state = clientConnStateConnected
m.stateMu.Unlock()
m.eio = _eio
m.resetParser()
m.closePacketQueue(m.eioPacketQueue)
m.cleanup()
m.subsMu.Lock()
activeMu.Lock()
if active {
m.subs = append(m.subs, func() {
activeMu.Lock()
active = false
activeMu.Unlock()
})
}
activeMu.Unlock()
m.subsMu.Unlock()
m.eioPacketQueue = newPacketQueue()
go m.eioPacketQueue.pollAndSend(_eio)
m.openHandlers.forEach(func(handler *ManagerOpenFunc) { (*handler)() }, true)
return
}
func (m *Manager) reconnect(recursed bool) {
m.debug.Log("`reconnect` called")
// recursed = Is this the first time we're running the reconnect method?
// In other words: are we recursing?
if !recursed {
m.connectMu.Lock()
defer m.connectMu.Unlock()
m.skipReconnectMu.RLock()
if m.skipReconnect {
m.skipReconnectMu.RUnlock()
m.debug.Log("Skipping reconnect")
return
}
m.skipReconnectMu.RUnlock()
}
// If the state is 'connected', 'connecting', or 'reconnecting', etc; don't try to connect.
//
// If the state is 'reconnecting' or 'connecting', there is another goroutine trying to connect.
// If the state is 'connected', there is nothing for this method to do.
m.stateMu.Lock()
if m.state != clientConnStateDisconnected {
m.stateMu.Unlock()
return
}
m.state = clientConnStateReconnecting
m.stateMu.Unlock()
attempts := m.backoff.attempts()
didAttemptsReachedMaxAttempts := m.reconnectionAttempts > 0 && attempts >= m.reconnectionAttempts
// Just in case
didAttemptsReachedMaxInt := m.reconnectionAttempts == 0 && attempts == math.MaxUint32
if didAttemptsReachedMaxAttempts || didAttemptsReachedMaxInt {
m.debug.Log("Maximum attempts reached. Attempts made so far", attempts)
m.backoff.reset()
m.stateMu.Lock()
m.state = clientConnStateDisconnected
m.stateMu.Unlock()
m.reconnectFailedHandlers.forEach(func(handler *ManagerReconnectFailedFunc) { (*handler)() }, true)
return
}
delay := m.backoff.duration()
m.debug.Log("Delay before reconnect attempt", delay)
time.Sleep(delay)
m.skipReconnectMu.RLock()
if m.skipReconnect {
m.skipReconnectMu.RUnlock()
m.debug.Log("Skipping reconnect")
return
}
m.skipReconnectMu.RUnlock()
attempts = m.backoff.attempts()
m.reconnectAttemptHandlers.forEach(func(handler *ManagerReconnectAttemptFunc) { (*handler)(attempts) }, true)
m.skipReconnectMu.RLock()
if m.skipReconnect {
m.skipReconnectMu.RUnlock()
m.debug.Log("Skipping reconnect")
return
}
m.skipReconnectMu.RUnlock()
m.debug.Log("Attempting to reconnect")
err := m.connect(true)
if err != nil {
m.debug.Log("Reconnect failed", err)
m.stateMu.Lock()
m.state = clientConnStateDisconnected
m.stateMu.Unlock()
m.reconnectErrorHandlers.forEach(func(handler *ManagerReconnectErrorFunc) { (*handler)(err) }, true)
m.reconnect(true)
return
}
m.debug.Log("Reconnected")
m.onReconnect()
}