-
Notifications
You must be signed in to change notification settings - Fork 453
/
conn.go
276 lines (249 loc) · 8.2 KB
/
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
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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package client
import (
"errors"
"math/rand"
"net"
"sync"
"time"
"github.com/m3db/m3/src/x/clock"
xio "github.com/m3db/m3/src/x/io"
"github.com/m3db/m3/src/x/retry"
"github.com/uber-go/tally"
)
const (
tcpProtocol = "tcp"
)
var (
errNoActiveConnection = errors.New("no active connection")
errInvalidConnection = errors.New("connection is invalid")
uninitWriter uninitializedWriter
)
type (
sleepFn func(time.Duration)
connectWithLockFn func() error
writeWithLockFn func([]byte) error
)
// connection is a persistent connection that retries establishing
// connection with exponential backoff if the connection goes down.
type connection struct {
metrics connectionMetrics
writeRetryOpts retry.Options
writer xio.ResettableWriter
connectWithLockFn connectWithLockFn
sleepFn sleepFn
nowFn clock.NowFn
conn *net.TCPConn
rngFn retry.RngFn
writeWithLockFn writeWithLockFn
addr string
maxDuration time.Duration
maxThreshold int
multiplier int
initThreshold int
threshold int
lastConnectAttemptNanos int64
writeTimeout time.Duration
connTimeout time.Duration
numFailures int
mtx sync.Mutex
keepAlive bool
}
// newConnection creates a new connection.
func newConnection(addr string, opts ConnectionOptions) *connection {
c := &connection{
addr: addr,
connTimeout: opts.ConnectionTimeout(),
writeTimeout: opts.WriteTimeout(),
keepAlive: opts.ConnectionKeepAlive(),
initThreshold: opts.InitReconnectThreshold(),
multiplier: opts.ReconnectThresholdMultiplier(),
maxThreshold: opts.MaxReconnectThreshold(),
maxDuration: opts.MaxReconnectDuration(),
writeRetryOpts: opts.WriteRetryOptions(),
rngFn: rand.New(rand.NewSource(time.Now().UnixNano())).Int63n,
nowFn: opts.ClockOptions().NowFn(),
sleepFn: time.Sleep,
threshold: opts.InitReconnectThreshold(),
writer: opts.RWOptions().ResettableWriterFn()(
uninitWriter,
xio.ResettableWriterOptions{WriteBufferSize: 0},
),
metrics: newConnectionMetrics(opts.InstrumentOptions().MetricsScope()),
}
c.connectWithLockFn = c.connectWithLock
c.writeWithLockFn = c.writeWithLock
return c
}
// Write sends data onto the connection, and attempts to re-establish
// connection if the connection is down.
func (c *connection) Write(data []byte) error {
var err error
c.mtx.Lock()
if c.conn == nil {
if err = c.checkReconnectWithLock(); err != nil {
c.numFailures++
c.mtx.Unlock()
return err
}
}
if err = c.writeAttemptWithLock(data); err == nil {
c.mtx.Unlock()
return nil
}
for i := 1; i <= c.writeRetryOpts.MaxRetries(); i++ {
if backoffDur := time.Duration(retry.BackoffNanos(
i,
c.writeRetryOpts.Jitter(),
c.writeRetryOpts.BackoffFactor(),
c.writeRetryOpts.InitialBackoff(),
c.writeRetryOpts.MaxBackoff(),
c.rngFn,
)); backoffDur > 0 {
c.sleepFn(backoffDur)
}
c.metrics.writeRetries.Inc(1)
if err = c.writeAttemptWithLock(data); err == nil {
c.mtx.Unlock()
return nil
}
}
c.numFailures++
c.mtx.Unlock()
return err
}
func (c *connection) Close() {
c.mtx.Lock()
c.closeWithLock()
c.mtx.Unlock()
}
// writeAttemptWithLock attempts to establish a new connection and writes raw bytes
// to the connection while holding the write lock.
// If the write succeeds, c.conn is guaranteed to be a valid connection on return.
// If the write fails, c.conn is guaranteed to be nil on return.
func (c *connection) writeAttemptWithLock(data []byte) error {
if c.conn == nil {
if err := c.connectWithLockFn(); err != nil {
return err
}
}
if err := c.writeWithLockFn(data); err != nil {
c.closeWithLock()
return err
}
return nil
}
func (c *connection) connectWithLock() error {
c.lastConnectAttemptNanos = c.nowFn().UnixNano()
conn, err := net.DialTimeout(tcpProtocol, c.addr, c.connTimeout)
if err != nil {
c.metrics.connectError.Inc(1)
return err
}
tcpConn := conn.(*net.TCPConn)
if err := tcpConn.SetKeepAlive(c.keepAlive); err != nil {
c.metrics.setKeepAliveError.Inc(1)
}
if c.conn != nil {
c.conn.Close() // nolint: errcheck
}
c.conn = tcpConn
c.writer.Reset(tcpConn)
return nil
}
func (c *connection) checkReconnectWithLock() error {
// If we haven't accumulated enough failures to warrant another reconnect
// and we haven't past the maximum duration since the last time we attempted
// to connect then we simply return false without reconnecting.
// If we exhausted maximum allowed failures then we will retry only based on
// maximum duration since the last attempt.
enoughFailuresToRetry := c.numFailures >= c.threshold
exhaustedMaxFailures := c.numFailures >= c.maxThreshold
sufficientTimePassed := c.nowFn().UnixNano()-c.lastConnectAttemptNanos >= c.maxDuration.Nanoseconds()
if !sufficientTimePassed && (exhaustedMaxFailures || !enoughFailuresToRetry) {
return errNoActiveConnection
}
err := c.connectWithLockFn()
if err == nil {
c.resetWithLock()
return nil
}
// Only raise the threshold when it is crossed, not when the max duration is reached.
if enoughFailuresToRetry && c.threshold < c.maxThreshold {
newThreshold := c.threshold * c.multiplier
if newThreshold > c.maxThreshold {
newThreshold = c.maxThreshold
}
c.threshold = newThreshold
}
return err
}
func (c *connection) writeWithLock(data []byte) error {
if err := c.conn.SetWriteDeadline(c.nowFn().Add(c.writeTimeout)); err != nil {
c.metrics.setWriteDeadlineError.Inc(1)
}
if _, err := c.writer.Write(data); err != nil {
c.metrics.writeError.Inc(1)
return err
}
if err := c.writer.Flush(); err != nil {
c.metrics.writeError.Inc(1)
return err
}
return nil
}
func (c *connection) resetWithLock() {
c.numFailures = 0
c.threshold = c.initThreshold
}
func (c *connection) closeWithLock() {
if c.conn != nil {
c.conn.Close() // nolint: errcheck
}
c.conn = nil
}
const (
errorMetric = "errors"
errorMetricType = "error-type"
)
type connectionMetrics struct {
connectError tally.Counter
writeError tally.Counter
writeRetries tally.Counter
setKeepAliveError tally.Counter
setWriteDeadlineError tally.Counter
}
func newConnectionMetrics(scope tally.Scope) connectionMetrics {
return connectionMetrics{
connectError: scope.Tagged(map[string]string{errorMetricType: "connect"}).
Counter(errorMetric),
writeError: scope.Tagged(map[string]string{errorMetricType: "write"}).
Counter(errorMetric),
writeRetries: scope.Tagged(map[string]string{"action": "write"}).Counter("retries"),
setKeepAliveError: scope.Tagged(map[string]string{errorMetricType: "tcp-keep-alive"}).
Counter(errorMetric),
setWriteDeadlineError: scope.Tagged(map[string]string{errorMetricType: "set-write-deadline"}).
Counter(errorMetric),
}
}
type uninitializedWriter struct{}
func (u uninitializedWriter) Write(p []byte) (int, error) { return 0, errInvalidConnection }
func (u uninitializedWriter) Close() error { return nil }