forked from Cepave/open-falcon-backend
/
conn_pool_manager.go
246 lines (206 loc) · 5.35 KB
/
conn_pool_manager.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
package conn_pool
import (
"fmt"
"net"
"net/rpc"
"sync"
"time"
)
// RpcCient, 要实现io.Closer接口
type RpcClient struct {
cli *rpc.Client
name string
}
func (this RpcClient) Name() string {
return this.name
}
func (this RpcClient) Closed() bool {
return this.cli == nil
}
func (this RpcClient) Close() error {
if this.cli != nil {
err := this.cli.Close()
this.cli = nil
return err
}
return nil
}
func (this RpcClient) Call(method string, args interface{}, reply interface{}) error {
return this.cli.Call(method, args, reply)
}
// ConnPools Manager
type SafeRpcConnPools struct {
sync.RWMutex
M map[string]*ConnPool
MaxConns int
MaxIdle int
ConnTimeout int
CallTimeout int
}
func CreateSafeRpcConnPools(maxConns, maxIdle, connTimeout, callTimeout int, cluster []string) *SafeRpcConnPools {
cp := &SafeRpcConnPools{M: make(map[string]*ConnPool), MaxConns: maxConns, MaxIdle: maxIdle,
ConnTimeout: connTimeout, CallTimeout: callTimeout}
ct := time.Duration(cp.ConnTimeout) * time.Millisecond
for _, address := range cluster {
if _, exist := cp.M[address]; exist {
continue
}
cp.M[address] = createOnePool(address, address, ct, maxConns, maxIdle)
}
return cp
}
func (this *SafeRpcConnPools) Proc() []string {
procs := []string{}
for _, cp := range this.M {
procs = append(procs, cp.Proc())
}
return procs
}
// 同步发送, 完成发送或超时后 才能返回
func (this *SafeRpcConnPools) Call(addr, method string, args interface{}, resp interface{}) error {
connPool, exists := this.Get(addr)
if !exists {
return fmt.Errorf("%s has no connection pool", addr)
}
conn, err := connPool.Fetch()
if err != nil {
return fmt.Errorf("%s get connection fail: conn %v, err %v. proc: %s", addr, conn, err, connPool.Proc())
}
rpcClient := conn.(RpcClient)
callTimeout := time.Duration(this.CallTimeout) * time.Millisecond
done := make(chan error, 1)
go func() {
done <- rpcClient.Call(method, args, resp)
}()
select {
case <-time.After(callTimeout):
connPool.ForceClose(conn)
return fmt.Errorf("%s, call timeout", addr)
case err = <-done:
if err != nil {
connPool.ForceClose(conn)
err = fmt.Errorf("%s, call failed, err %v. proc: %s", addr, err, connPool.Proc())
} else {
connPool.Release(conn)
}
return err
}
}
func (this *SafeRpcConnPools) Get(address string) (*ConnPool, bool) {
this.RLock()
defer this.RUnlock()
p, exists := this.M[address]
return p, exists
}
func (this *SafeRpcConnPools) Destroy() {
this.Lock()
defer this.Unlock()
addresses := make([]string, 0, len(this.M))
for address := range this.M {
addresses = append(addresses, address)
}
for _, address := range addresses {
this.M[address].Destroy()
delete(this.M, address)
}
}
func createOnePool(name string, address string, connTimeout time.Duration, maxConns int, maxIdle int) *ConnPool {
p := NewConnPool(name, address, maxConns, maxIdle)
p.New = func(connName string) (NConn, error) {
_, err := net.ResolveTCPAddr("tcp", p.Address)
if err != nil {
//log.Println(p.Address, "format error", err)
return nil, err
}
conn, err := net.DialTimeout("tcp", p.Address, connTimeout)
if err != nil {
//log.Printf("new conn fail, addr %s, err %v", p.Address, err)
return nil, err
}
return RpcClient{cli: rpc.NewClient(conn), name: connName}, nil
}
return p
}
// TSDB
type TsdbClient struct {
cli net.Conn
name string
}
func (this TsdbClient) Name() string {
return this.name
}
func (this TsdbClient) Closed() bool {
return this.cli == nil
}
func (this TsdbClient) Close() error {
if this.cli != nil {
err := this.cli.Close()
this.cli = nil
return err
}
return nil
}
func newTsdbConnPool(address string, maxConns int, maxIdle int, connTimeout int) *ConnPool {
pool := NewConnPool("tsdb", address, maxConns, maxIdle)
pool.New = func(name string) (NConn, error) {
_, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout("tcp", address, time.Duration(connTimeout)*time.Millisecond)
if err != nil {
return nil, err
}
return TsdbClient{conn, name}, nil
}
return pool
}
type TsdbConnPoolHelper struct {
p *ConnPool
maxConns int
maxIdle int
connTimeout int
callTimeout int
address string
}
func NewTsdbConnPoolHelper(address string, maxConns, maxIdle, connTimeout, callTimeout int) *TsdbConnPoolHelper {
return &TsdbConnPoolHelper{
p: newTsdbConnPool(address, maxConns, maxIdle, connTimeout),
maxConns: maxConns,
maxIdle: maxIdle,
connTimeout: connTimeout,
callTimeout: callTimeout,
address: address,
}
}
func (this *TsdbConnPoolHelper) Send(data []byte) (err error) {
conn, err := this.p.Fetch()
if err != nil {
return fmt.Errorf("get connection fail: err %v. proc: %s", err, this.p.Proc())
}
cli := conn.(TsdbClient).cli
done := make(chan error, 1)
go func() {
_, err = cli.Write(data)
done <- err
}()
select {
case <-time.After(time.Duration(this.callTimeout) * time.Millisecond):
this.p.ForceClose(conn)
return fmt.Errorf("%s, call timeout", this.address)
case err = <-done:
if err != nil {
this.p.ForceClose(conn)
err = fmt.Errorf("%s, call failed, err %v. proc: %s", this.address, err, this.p.Proc())
} else {
this.p.Release(conn)
}
return err
}
return
}
func (this *TsdbConnPoolHelper) Destroy() {
if this.p != nil {
this.p.Destroy()
}
}