-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
232 lines (206 loc) · 5.62 KB
/
cluster.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
package exnet
import (
"context"
"net"
"sync/atomic"
"time"
)
var (
defaultTCPKeepAlive = true
defaultTCPKeepAlivePeriod = 3 * time.Second
defaultTCPLinger = 1
defaultTCPNoDelay = true
)
// Cluster contain service info
type Cluster struct {
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
AddressPicker AddressPicker
connpool ConnPool
tcpKeepAlive bool
tcpKeepAlivePeriod time.Duration
tcpLinger int
tcpNoDelay bool
// metrics
metricDialDirect int64
metricDialPoolReuse int64
}
// ClusterConfig expose config for cluster
type ClusterConfig struct {
// DialTimeout is the default timeout when call Dial
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// connection pool settings
PoolConfig *ConnPoolConfig
UseAsyncPool bool
}
// AddressPicker interface to get an address
type AddressPicker interface {
Addr() net.Addr
}
// AddressPickerConcern interface to concern the address usage
type AddressPickerConcern interface {
// Connected will be called when an address is connected
Connected(net.Addr)
// Disconnected will be called when an address is disconnected
Disconnected(net.Addr)
// Failure will be called when an address can't be connected
Failure(net.Addr, error)
}
// NewCluster create new cluster with config and default options.
func NewCluster(conf *ClusterConfig) *Cluster {
c := &Cluster{
DialTimeout: conf.DialTimeout,
ReadTimeout: conf.ReadTimeout,
WriteTimeout: conf.WriteTimeout,
AddressPicker: nil,
tcpKeepAlive: defaultTCPKeepAlive,
tcpKeepAlivePeriod: defaultTCPKeepAlivePeriod,
tcpLinger: defaultTCPLinger,
tcpNoDelay: defaultTCPNoDelay,
}
if conf.PoolConfig != nil {
if conf.UseAsyncPool {
c.connpool = NewAsyncConnPool(conf.PoolConfig)
} else {
c.connpool = NewSyncConnPool(conf.PoolConfig)
}
}
return c
}
// Dial create an empty context and dial with it
func (c *Cluster) Dial(network, addr string) (net.Conn, error) {
return c.DialContext(context.Background(), network, addr)
}
// DialContext dial and return an exnet.Conn, network and address is useless, we use
// AddressPicker to get one.
func (c *Cluster) DialContext(ctx context.Context, _, _ string) (net.Conn, error) {
if c.connpool != nil {
if conn := c.connpool.Get(); conn != nil {
if c.resetDeadlines(conn) == nil {
atomic.AddInt64(&c.metricDialPoolReuse, 1)
return &Conn{_conn: conn, closer: c}, nil
}
_ = conn.Close()
}
}
atomic.AddInt64(&c.metricDialDirect, 1)
return c.dialContextDirect(ctx)
}
func (c *Cluster) dialContextDirect(ctx context.Context) (net.Conn, error) {
addr := c.AddressPicker.Addr()
dialer := &Dialer{
dialer: &net.Dialer{
Timeout: c.DialTimeout,
},
}
conn, err := dialer.DialContext(ctx, addr.Network(), addr.String())
// concern
if apc, ok := c.AddressPicker.(AddressPickerConcern); ok {
if err == nil {
apc.Connected(addr)
} else {
apc.Failure(addr, err)
}
}
if err != nil {
return nil, err
}
// SetSockOpt for tcp connection
switch ulconn := UnwrapConn(conn).(type) {
case *net.TCPConn:
if err = c.tcpsetsockopt(ulconn); err != nil {
ulconn.Close()
return nil, err
}
}
err = c.resetDeadlines(conn)
if err != nil {
_ = UnwrapConn(conn).Close()
return nil, err
}
return conn, nil
}
func (c *Cluster) resetDeadlines(conn net.Conn) error {
var err error
// TODO: Set REAL Deadline
err = conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
if err != nil {
return err
}
err = conn.SetDeadline(time.Now().Add(c.ReadTimeout).Add(c.WriteTimeout))
if err != nil {
return err
}
return nil
}
// Close conn closer
func (c *Cluster) Close(conn net.Conn) error {
if exconn, ok := conn.(*Conn); ok {
if exconn.err != nil {
return UnwrapConn(conn).Close()
}
}
if c.connpool != nil {
c.connpool.Put(conn)
return nil
}
return UnwrapConn(conn).Close()
}
// TCPSetKeepAlive change keep-alive when setsockopt after dial.
// WARN: Keep-alive is enable by default, ensure you know what you are doing
// when you call this function and change it
func (c *Cluster) TCPSetKeepAlive(keepAlive bool) {
c.tcpKeepAlive = keepAlive
}
// TCPSetKeepAlivePeriod change keep-alive period when setsockopt after dial.
// WARN: Keep-alive period is enable 3 seconds, ensure you know what you are doing
// when you call this function and change it
func (c *Cluster) TCPSetKeepAlivePeriod(d time.Duration) {
c.tcpKeepAlivePeriod = d
}
// TCPSetLinger change linger when setsockopt after dial
// WARN: Linger is enable by default, ensure you know what you are doing
// when you call this function and change it
func (c *Cluster) TCPSetLinger(linger int) {
c.tcpLinger = linger
}
// TCPSetNoDelay set NoDelay when setsockopt after dial
// WARN: NoDelay is enabled by default, ensure you know what you are doing
// when you call this function and change it
func (c *Cluster) TCPSetNoDelay(noDelay bool) {
c.tcpNoDelay = noDelay
}
// tcpsetsockopt
func (c *Cluster) tcpsetsockopt(conn *net.TCPConn) error {
var err error
// Keep-Alive
err = conn.SetKeepAlive(c.tcpKeepAlive)
if err != nil {
return err
}
// Keep-Alive Period
err = conn.SetKeepAlivePeriod(c.tcpKeepAlivePeriod)
if err != nil {
return err
}
// Linger
err = conn.SetLinger(c.tcpLinger)
if err != nil {
return err
}
// NoDelay
err = conn.SetNoDelay(c.tcpNoDelay)
if err != nil {
return err
}
return nil
}
func (c *Cluster) Metrics() map[string]int64 {
return map[string]int64{
"dial_direct": atomic.LoadInt64(&c.metricDialDirect),
"dial_pool_reuse": atomic.LoadInt64(&c.metricDialPoolReuse),
}
}