/
redialer.go
113 lines (102 loc) · 2.65 KB
/
redialer.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
package config
import (
"fmt"
"net"
"sync"
"syscall"
"time"
)
// Dial calls net.Dial with the specified arguments. If they succeed and ntype
// is not "unix", the resulting connection is wrapped to redial the address if
// the connection fails during a read or write (ECONNRESET or ECONNABORTED).
//
// If redial succeeds, the failed Read or Write operation is transparently
// retried; otherwise the original error is propagated to the caller.
func Dial(ntype, addr string) (net.Conn, error) {
c, err := net.Dial(ntype, addr)
if err != nil {
return nil, err
} else if ntype == "unix" {
return c, nil
}
return &redialConn{
Conn: c,
net: ntype,
addr: addr,
maxAttempts: 4,
grace: 30 * time.Second,
pause: 50 * time.Millisecond,
lastSuccess: time.Now(),
}, nil
}
type redialConn struct {
// Must hold mu exclusively to read or write the connection.
mu sync.Mutex
net.Conn
net, addr string
maxAttempts int // total attempts allowed per successful connection
numAttempts int // attempts since latest grace interval
grace time.Duration // interval beyond which attempts are reset
pause time.Duration // interval between redial attempts
lastSuccess time.Time // last successful dial
}
func (r *redialConn) getConn() net.Conn {
r.mu.Lock()
defer r.mu.Unlock()
return r.Conn
}
func (r *redialConn) tryRedial(old net.Conn) error {
r.mu.Lock()
defer r.mu.Unlock()
// If the connection was successfully replaced since we read the original
// value, we can short-circuit here.
if r.Conn != old {
return nil
}
if time.Since(r.lastSuccess) > r.grace {
r.numAttempts = 0
} else if r.numAttempts >= r.maxAttempts {
return fmt.Errorf("maximum %d redials exhausted", r.maxAttempts)
}
if r.numAttempts > 0 {
time.Sleep(time.Duration(r.numAttempts) * r.pause)
}
r.numAttempts++
c, err := net.Dial(r.net, r.addr)
if err != nil {
return err
}
r.Conn = c
r.lastSuccess = time.Now()
return nil
}
func (r *redialConn) Read(data []byte) (int, error) {
for {
conn := r.getConn()
nr, err := conn.Read(data)
if err != nil {
if isRedialableError(err) && r.tryRedial(conn) == nil {
continue // retry the failed operation
}
}
return nr, err
}
}
func (r *redialConn) Write(data []byte) (int, error) {
for {
conn := r.getConn()
nw, err := conn.Write(data)
if err != nil {
if isRedialableError(err) && r.tryRedial(conn) == nil {
continue // retry the failed operation
}
}
return nw, err
}
}
func isRedialableError(err error) bool {
if e, ok := err.(syscall.Errno); ok {
return e == syscall.ECONNRESET || e == syscall.ECONNABORTED
}
return false
}