forked from netbrain/goautosocket
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcp_client.go
271 lines (229 loc) · 6.86 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
// Copyright © 2015 Clement 'cmc' Rey <cr.rey.clement@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gas
import (
"io"
"math"
"net"
"sync"
"sync/atomic"
"syscall"
"time"
)
// ----------------------------------------------------------------------------
// status enum
const (
statusOnline = iota
statusOffline = iota
statusReconnecting = iota
)
// ----------------------------------------------------------------------------
// 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
status int32
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{},
status: 0,
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.
//
// TODO: keep old socket configuration (timeout, linger...).
func (c *TCPClient) reconnect() error {
// set the shared status to 'reconnecting'
// if it's already the case, return early, something's already trying to
// reconnect
if !atomic.CompareAndSwapInt32(&c.status, statusOffline, statusReconnecting) {
return nil
}
raddr := c.TCPConn.RemoteAddr()
conn, err := net.DialTCP(raddr.Network(), nil, raddr.(*net.TCPAddr))
if err != nil {
// reset shared status to offline
defer atomic.StoreInt32(&c.status, statusOffline)
return err
}
// set new TCP socket
c.TCPConn.Close()
c.TCPConn = conn
// we're back online, set shared status accordingly
atomic.StoreInt32(&c.status, statusOnline)
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) {
// protect conf values (retryInterval, maxRetries...)
c.lock.RLock()
defer c.lock.RUnlock()
for i := 0; i < c.maxRetries; i++ {
if atomic.LoadInt32(&c.status) == statusOnline {
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 {
atomic.StoreInt32(&c.status, statusOffline)
} else {
return n, err
}
default:
if err.Error() == "EOF" {
atomic.StoreInt32(&c.status, statusOffline)
} else {
return n, err
}
}
} else if atomic.LoadInt32(&c.status) == statusOffline {
if err := c.reconnect(); err != nil {
return -1, err
}
}
// exponential backoff
if i < (c.maxRetries - 1) {
time.Sleep(c.retryInterval * time.Duration(math.Pow(2, float64(i))))
}
}
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) {
// protect conf values (retryInterval, maxRetries...)
c.lock.RLock()
defer c.lock.RUnlock()
for i := 0; i < c.maxRetries; i++ {
if atomic.LoadInt32(&c.status) == statusOnline {
n, err := c.TCPConn.ReadFrom(r)
if err == nil {
return n, err
}
atomic.StoreInt32(&c.status, statusOffline)
} else if atomic.LoadInt32(&c.status) == statusOffline {
if err := c.reconnect(); err != nil {
return -1, err
}
}
// exponential backoff
if i < (c.maxRetries - 1) {
time.Sleep(c.retryInterval * time.Duration(math.Pow(2, float64(i))))
}
}
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) {
// protect conf values (retryInterval, maxRetries...)
c.lock.RLock()
defer c.lock.RUnlock()
for i := 0; i < c.maxRetries; i++ {
if atomic.LoadInt32(&c.status) == statusOnline {
n, err := c.TCPConn.Write(b)
if err == nil {
return n, err
}
atomic.StoreInt32(&c.status, statusOffline)
} else if atomic.LoadInt32(&c.status) == statusOffline {
if err := c.reconnect(); err != nil {
return -1, err
}
}
// exponential backoff
if i < (c.maxRetries - 1) {
time.Sleep(c.retryInterval * time.Duration(math.Pow(2, float64(i))))
}
}
return -1, ErrMaxRetries
}