-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtcp_client.go
311 lines (273 loc) · 6.93 KB
/
tcp_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
// Copyright © 2015 Clement 'cmc' Rey <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gas
import (
"io"
"net"
"sync"
"syscall"
"time"
)
// ----------------------------------------------------------------------------
// TCPClient provides a TCP connection with auto-reconnect capabilities.
//
// It embeds a *net.TCPConn and thus implements the net.Conn interface.
//
// Use the SetMaxRetries() and SetRetryInterval() methods to configure retry
// values; otherwise they default to maxRetries=5 and retryInterval=100ms.
//
// TCPClient can be safely used from multiple goroutines.
type TCPClient struct {
*net.TCPConn
lock sync.RWMutex
maxRetries int
retryInterval time.Duration
}
// Dial returns a new net.Conn.
//
// The new client connects to the remote address `raddr` on the network `network`,
// which must be "tcp", "tcp4", or "tcp6".
//
// This complements net package's Dial function.
func Dial(network, addr string) (net.Conn, error) {
raddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return DialTCP(network, nil, raddr)
}
// DialTCP returns a new *TCPClient.
//
// The new client connects to the remote address `raddr` on the network `network`,
// which must be "tcp", "tcp4", or "tcp6".
// If `laddr` is not nil, it is used as the local address for the connection.
//
// This overrides net.TCPConn's DialTCP function.
func DialTCP(network string, laddr, raddr *net.TCPAddr) (*TCPClient, error) {
conn, err := net.DialTCP(network, laddr, raddr)
if err != nil {
return nil, err
}
return &TCPClient{
TCPConn: conn,
lock: sync.RWMutex{},
maxRetries: 10,
retryInterval: 10 * time.Millisecond,
}, nil
}
// ----------------------------------------------------------------------------
// SetMaxRetries sets the retry limit for the TCPClient.
//
// Assuming i is the current retry iteration, the total sleep time is
// t = retryInterval * (2^i)
//
// This function completely Lock()s the TCPClient.
func (c *TCPClient) SetMaxRetries(maxRetries int) {
c.lock.Lock()
defer c.lock.Unlock()
c.maxRetries = maxRetries
}
// GetMaxRetries gets the retry limit for the TCPClient.
//
// Assuming i is the current retry iteration, the total sleep time is
// t = retryInterval * (2^i)
func (c *TCPClient) GetMaxRetries() int {
c.lock.RLock()
defer c.lock.RUnlock()
return c.maxRetries
}
// SetRetryInterval sets the retry interval for the TCPClient.
//
// Assuming i is the current retry iteration, the total sleep time is
// t = retryInterval * (2^i)
//
// This function completely Lock()s the TCPClient.
func (c *TCPClient) SetRetryInterval(retryInterval time.Duration) {
c.lock.Lock()
defer c.lock.Unlock()
c.retryInterval = retryInterval
}
// GetRetryInterval gets the retry interval for the TCPClient.
//
// Assuming i is the current retry iteration, the total sleep time is
// t = retryInterval * (2^i)
func (c *TCPClient) GetRetryInterval() time.Duration {
c.lock.RLock()
defer c.lock.RUnlock()
return c.retryInterval
}
// ----------------------------------------------------------------------------
// reconnect builds a new TCP connection to replace the embedded *net.TCPConn.
//
// This function completely Lock()s the TCPClient.
//
// TODO: keep old socket configuration (timeout, linger...).
func (c *TCPClient) reconnect() error {
c.lock.Lock()
defer c.lock.Unlock()
raddr := c.TCPConn.RemoteAddr()
conn, err := net.DialTCP(raddr.Network(), nil, raddr.(*net.TCPAddr))
if err != nil {
return err
}
c.TCPConn.Close()
c.TCPConn = conn
return nil
}
// ----------------------------------------------------------------------------
// Read wraps net.TCPConn's Read method with reconnect capabilities.
//
// It will return ErrMaxRetries if the retry limit is reached.
func (c *TCPClient) Read(b []byte) (int, error) {
c.lock.RLock()
defer c.lock.RUnlock()
disconnected := false
t := c.retryInterval
for i := 0; i < c.maxRetries; i++ {
if disconnected {
time.Sleep(t)
t *= 2
c.lock.RUnlock()
if err := c.reconnect(); err != nil {
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNREFUSED {
disconnected = true
c.lock.RLock()
continue
}
return -1, err
default:
return -1, err
}
} else {
disconnected = false
}
c.lock.RLock()
}
n, err := c.TCPConn.Read(b)
if err == nil {
return n, err
}
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNRESET ||
e.Err.(syscall.Errno) == syscall.EPIPE {
disconnected = true
} else {
return n, err
}
default:
if err.Error() == "EOF" {
disconnected = true
} else {
return n, err
}
}
t *= 2
}
return -1, ErrMaxRetries
}
// ReadFrom wraps net.TCPConn's ReadFrom method with reconnect capabilities.
//
// It will return ErrMaxRetries if the retry limit is reached.
func (c *TCPClient) ReadFrom(r io.Reader) (int64, error) {
c.lock.RLock()
defer c.lock.RUnlock()
disconnected := false
t := c.retryInterval
for i := 0; i < c.maxRetries; i++ {
if disconnected {
time.Sleep(t)
t *= 2
c.lock.RUnlock()
if err := c.reconnect(); err != nil {
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNREFUSED {
disconnected = true
c.lock.RLock()
continue
}
return -1, err
default:
return -1, err
}
} else {
disconnected = false
}
c.lock.RLock()
}
n, err := c.TCPConn.ReadFrom(r)
if err == nil {
return n, err
}
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNRESET ||
e.Err.(syscall.Errno) == syscall.EPIPE {
disconnected = true
} else {
return n, err
}
default:
if err.Error() == "EOF" {
disconnected = true
} else {
return n, err
}
}
t *= 2
}
return -1, ErrMaxRetries
}
// Write wraps net.TCPConn's Write method with reconnect capabilities.
//
// It will return ErrMaxRetries if the retry limit is reached.
func (c *TCPClient) Write(b []byte) (int, error) {
c.lock.RLock()
defer c.lock.RUnlock()
disconnected := false
t := c.retryInterval
for i := 0; i < c.maxRetries; i++ {
if disconnected {
time.Sleep(t)
t *= 2
c.lock.RUnlock()
if err := c.reconnect(); err != nil {
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNREFUSED {
disconnected = true
c.lock.RLock()
continue
}
return -1, err
default:
return -1, err
}
} else {
disconnected = false
}
c.lock.RLock()
}
n, err := c.TCPConn.Write(b)
if err == nil {
return n, err
}
switch e := err.(type) {
case *net.OpError:
if e.Err.(syscall.Errno) == syscall.ECONNRESET ||
e.Err.(syscall.Errno) == syscall.EPIPE {
disconnected = true
} else {
return n, err
}
default:
return n, err
}
}
return -1, ErrMaxRetries
}