forked from open-falcon/falcon-plus
/
rpc_backends.go
169 lines (142 loc) · 4.52 KB
/
rpc_backends.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
// Copyright 2017 Xiaomi, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backend_pool
import (
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"sync"
"time"
connp "github.com/toolkits/conn_pool"
rpcpool "github.com/toolkits/conn_pool/rpc_conn_pool"
)
// ConnPools Manager
type SafeRpcConnPools struct {
sync.RWMutex
M map[string]*connp.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]*connp.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] = createOneRpcPool(address, address, ct, maxConns, maxIdle)
}
return cp
}
func CreateSafeJsonrpcConnPools(maxConns, maxIdle, connTimeout, callTimeout int, cluster []string) *SafeRpcConnPools {
cp := &SafeRpcConnPools{M: make(map[string]*connp.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] = createOneJsonrpcPool(address, address, ct, maxConns, maxIdle)
}
return cp
}
// 同步发送, 完成发送或超时后 才能返回
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.(*rpcpool.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) (*connp.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 (this *SafeRpcConnPools) Proc() []string {
procs := []string{}
for _, cp := range this.M {
procs = append(procs, cp.Proc())
}
return procs
}
func createOneRpcPool(name string, address string, connTimeout time.Duration, maxConns int, maxIdle int) *connp.ConnPool {
p := connp.NewConnPool(name, address, int32(maxConns), int32(maxIdle))
p.New = func(connName string) (connp.NConn, error) {
_, err := net.ResolveTCPAddr("tcp", p.Address)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout("tcp", p.Address, connTimeout)
if err != nil {
return nil, err
}
return rpcpool.NewRpcClient(rpc.NewClient(conn), connName), nil
}
return p
}
func createOneJsonrpcPool(name string, address string, connTimeout time.Duration, maxConns int, maxIdle int) *connp.ConnPool {
p := connp.NewConnPool(name, address, int32(maxConns), int32(maxIdle))
p.New = func(connName string) (connp.NConn, error) {
_, err := net.ResolveTCPAddr("tcp", p.Address)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout("tcp", p.Address, connTimeout)
if err != nil {
return nil, err
}
return rpcpool.NewRpcClientWithCodec(jsonrpc.NewClientCodec(conn), connName), nil
}
return p
}