forked from go-chassis/go-chassis
/
highway_base_client.go
288 lines (255 loc) · 6.5 KB
/
highway_base_client.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
277
278
279
280
281
282
283
284
285
286
287
288
package highway
import (
"crypto/tls"
"errors"
"github.com/go-chassis/go-chassis/core/lager"
"net"
"sync"
"time"
)
//ConnParams highway connect parmas
type ConnParams struct {
Addr string
TLSConfig *tls.Config
Timeout time.Duration
ConnNum int
}
//BaseClient highway base client
type BaseClient struct {
addr string
mtx sync.Mutex
mapMutex sync.Mutex
msgWaitRspMap map[uint64]*InvocationContext
highwayConns []*ClientConnection
closed bool
connParams *ConnParams
}
//CachedClients client cache
var CachedClients *ClientMgr
func init() {
CachedClients = newClientMgr()
}
//InvocationContext Highway context
type InvocationContext struct {
Req *Request
Rsp *Response
Wait *chan int
once sync.Once
}
//Done Notify done.
func (ctx *InvocationContext) Done() {
ctx.once.Do(func() {
close(*ctx.Wait)
})
}
//ClientMgr client manage
type ClientMgr struct {
mapMutex sync.Mutex
clients map[string]*BaseClient
}
func newClientMgr() *ClientMgr {
tmp := new(ClientMgr)
tmp.clients = make(map[string]*BaseClient)
return tmp
}
//GetClient Obtain client
func (mgr *ClientMgr) GetClient(connParmas *ConnParams) (*BaseClient, error) {
mgr.mapMutex.Lock()
defer mgr.mapMutex.Unlock()
if tmp, ok := mgr.clients[connParmas.Addr]; ok {
if !tmp.Closed() {
//GetClient from cached addr
return tmp, nil
}
delete(mgr.clients, connParmas.Addr)
}
lager.Logger.Info("GetClient from new open addr:" + connParmas.Addr)
tmp := newHighwayBaseClient(connParmas)
err := tmp.Open()
if err != nil {
return nil, err
}
mgr.clients[connParmas.Addr] = tmp
return tmp, nil
}
func newHighwayBaseClient(connParmas *ConnParams) *BaseClient {
tmp := &BaseClient{}
tmp.addr = connParmas.Addr
tmp.closed = true
tmp.connParams = connParmas
tmp.msgWaitRspMap = make(map[uint64]*InvocationContext)
return tmp
}
//GetAddr Obtain the address
func (baseClient *BaseClient) GetAddr() string {
return baseClient.addr
}
func (baseClient *BaseClient) makeConnection() (*ClientConnection, error) {
var baseConn net.Conn
var errDial error
if baseClient.connParams.TLSConfig != nil {
dialer := &net.Dialer{Timeout: baseClient.connParams.Timeout * time.Second}
baseConn, errDial = tls.DialWithDialer(dialer, "tcp", baseClient.addr, baseClient.connParams.TLSConfig)
} else {
baseConn, errDial = net.DialTimeout("tcp", baseClient.addr, baseClient.connParams.Timeout*time.Second)
}
if errDial != nil {
lager.Logger.Error("the addr: "+baseClient.addr, errDial)
return nil, errDial
}
highwayConn := NewHighwayClientConnection(baseConn, baseClient)
err := highwayConn.Open()
if err != nil {
lager.Logger.Error("highwayConn open: "+baseClient.addr, errDial)
return nil, err
}
return highwayConn, nil
}
func (baseClient *BaseClient) initConns() error {
if baseClient.connParams.ConnNum == 0 {
baseClient.connParams.ConnNum = 4
}
baseClient.highwayConns = make([]*ClientConnection, baseClient.connParams.ConnNum)
for i := 0; i < baseClient.connParams.ConnNum; i++ {
higwayConn, err := baseClient.makeConnection()
if err != nil {
return err
}
baseClient.highwayConns[i] = higwayConn
}
return nil
}
//Open client
func (baseClient *BaseClient) Open() error {
baseClient.mtx.Lock()
defer baseClient.mtx.Unlock()
err := baseClient.initConns()
if err != nil {
baseClient.clearConns()
return err
}
baseClient.closed = false
return nil
}
//Close client
func (baseClient *BaseClient) Close() {
baseClient.mtx.Lock()
defer baseClient.mtx.Unlock()
baseClient.close()
}
//close client, no mutex
func (baseClient *BaseClient) close() {
if baseClient.closed {
return
}
baseClient.mapMutex.Lock()
for _, v := range baseClient.msgWaitRspMap {
v.Done()
}
baseClient.msgWaitRspMap = make(map[uint64]*InvocationContext)
baseClient.mapMutex.Unlock()
baseClient.clearConns()
baseClient.closed = true
}
func (baseClient *BaseClient) clearConns() {
for i := 0; i < baseClient.connParams.ConnNum; i++ {
conn := baseClient.highwayConns[i]
if conn != nil {
conn.Close()
baseClient.highwayConns[i] = nil
}
}
}
//AddWaitMsg add wait msg
func (baseClient *BaseClient) AddWaitMsg(msgID uint64, result *InvocationContext) {
baseClient.mapMutex.Lock()
if baseClient.msgWaitRspMap != nil {
baseClient.msgWaitRspMap[msgID] = result
}
baseClient.mapMutex.Unlock()
}
//RemoveWaitMsg remove wait msg
func (baseClient *BaseClient) RemoveWaitMsg(msgID uint64) {
baseClient.mapMutex.Lock()
if baseClient.msgWaitRspMap != nil {
delete(baseClient.msgWaitRspMap, msgID)
}
baseClient.mapMutex.Unlock()
}
//Send send msg
func (baseClient *BaseClient) Send(req *Request, rsp *Response, timeout time.Duration) error {
if baseClient.closed {
baseClient.mtx.Lock()
if baseClient.closed {
baseClient.mtx.Unlock()
return errors.New("client is closed")
}
baseClient.mtx.Unlock()
}
msgID := req.MsgID
idx := msgID % uint64(baseClient.connParams.ConnNum)
highwayConn := baseClient.highwayConns[idx]
if highwayConn == nil || highwayConn.Closed() {
baseClient.mtx.Lock()
highwayConn = baseClient.highwayConns[idx]
if highwayConn == nil || highwayConn.Closed() {
highwayConnTmp, err := baseClient.makeConnection()
if err != nil {
baseClient.mtx.Unlock()
return err
}
highwayConn = highwayConnTmp
baseClient.highwayConns[idx] = highwayConn
}
baseClient.mtx.Unlock()
}
if req.TwoWay {
wait := make(chan int)
ctx := &InvocationContext{Req: req, Rsp: rsp, Wait: &wait}
baseClient.AddWaitMsg(msgID, ctx)
err := highwayConn.AsyncSendMsg(ctx)
if err != nil {
baseClient.RemoveWaitMsg(msgID)
rsp.Err = err.Error()
lager.Logger.Error("AsyncSendMsg err:", err)
return err
}
var bTimeout bool
select {
case <-wait:
bTimeout = false
case <-time.After(timeout * time.Second):
bTimeout = true
}
baseClient.RemoveWaitMsg(msgID)
if bTimeout {
ctx.Done()
rsp.Err = "Client send timeout"
return errors.New("Client send timeout")
}
if ctx.Rsp.Status != Ok {
return errors.New(ctx.Rsp.Err)
}
} else {
// Respond of postMsg is needless
err := highwayConn.PostMsg(req)
if err != nil {
lager.Logger.Error("PostMsg err:", err)
return err
}
}
return nil
}
//GetWaitMsg get wait message
func (baseClient *BaseClient) GetWaitMsg(msgID uint64) *InvocationContext {
baseClient.mapMutex.Lock()
defer baseClient.mapMutex.Unlock()
if _, ok := baseClient.msgWaitRspMap[msgID]; ok {
return baseClient.msgWaitRspMap[msgID]
}
return nil
}
//Closed client status
func (baseClient *BaseClient) Closed() bool {
return baseClient.closed
}