-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
528 lines (489 loc) · 12.2 KB
/
transport.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
)
const (
//DefaultMaxConnsPerHost is the default value of Transport's MaxConnsPerHost.
DefaultMaxConnsPerHost = 1
//DefaultMaxIdleConnsPerHost is the default value of Transport's MaxIdleConnsPerHost.
DefaultMaxIdleConnsPerHost = 1
//DefaultKeepAlive is the default value of Transport's KeepAlive.
DefaultKeepAlive = 90 * time.Second
//DefaultIdleConnTimeout is the default value of Transport's IdleConnTimeout.
DefaultIdleConnTimeout = 60 * time.Second
defaultRunTicker = time.Second
)
var (
// MaxConnsPerHost optionally limits the total number of
// connections per host, including connections in the dialing,
// active, and idle states. On limit violation, dials will block.
MaxConnsPerHost = DefaultMaxConnsPerHost
// MaxIdleConnsPerHost controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost = DefaultMaxIdleConnsPerHost
// KeepAlive specifies the maximum amount of time keeping the active connections in the Transport's conns.
KeepAlive = DefaultKeepAlive
// IdleConnTimeout specifies the maximum amount of time keeping the idle connections in the Transport's idleConns.
IdleConnTimeout = DefaultIdleConnTimeout
runTicker = defaultRunTicker
// ErrDial is returned when dial failed.
ErrDial = errors.New("dial failed")
)
// RoundTripper is an interface representing the ability to execute a
// single RPC transaction, obtaining the Response for a given Request.
type RoundTripper interface {
RoundTrip(addr string, call *Call) *Call
Go(addr, serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call
Call(addr, serviceMethod string, args interface{}, reply interface{}) error
CallWithContext(ctx context.Context, addr string, serviceMethod string, args interface{}, reply interface{}) error
NewStream(addr, key string) (Stream, error)
Ping(addr string) error
Close() error
}
//Transport defines the struct of transport
type Transport struct {
connsMu sync.Mutex
once sync.Once
idleConns map[string]*connQueue
conns map[string]*conns
MaxConnsPerHost int
MaxIdleConnsPerHost int
KeepAlive time.Duration
IdleConnTimeout time.Duration
now time.Time
Network string
Codec string
Dial func(network, address, codec string) (*Conn, error)
Options *Options
DialWithOptions func(address string, opts *Options) (*Conn, error)
running bool
done chan bool
closed uint32
ticker time.Duration
}
// DefaultTransport is a default RPC transport.
var DefaultTransport = &Transport{
MaxConnsPerHost: MaxConnsPerHost,
MaxIdleConnsPerHost: MaxIdleConnsPerHost,
KeepAlive: KeepAlive,
IdleConnTimeout: IdleConnTimeout,
Network: "tcp",
Codec: "json",
Dial: Dial,
DialWithOptions: DialWithOptions,
}
// RoundTrip executes a single RPC transaction, returning
// a Response for the provided Request.
func (t *Transport) RoundTrip(addr string, call *Call) *Call {
done := call.Done
done = checkDone(done)
call.Done = done
conn, err := t.getConn(addr)
if err != nil {
call.Error = err
call.done()
return call
}
conn.RoundTrip(call)
conn.lastTime = t.now
checkPersistConnErr(call.Error, conn)
return call
}
// Go invokes the function asynchronously. It returns the Call structure representing
// the invocation. The done channel will signal when the call is complete by returning
// the same Call object. If done is nil, Go will allocate a new channel.
// If non-nil, done must be buffered or Go will deliberately crash.
func (t *Transport) Go(addr, serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call {
conn, err := t.getConn(addr)
if err != nil {
call := new(Call)
call.ServiceMethod = serviceMethod
call.Args = args
call.Reply = reply
done = checkDone(done)
call.Done = done
call.Error = err
call.done()
return call
}
call := conn.Go(serviceMethod, args, reply, done)
conn.lastTime = t.now
checkPersistConnErr(call.Error, conn)
return call
}
// Call invokes the named function, waits for it to complete, and returns its error status.
func (t *Transport) Call(addr, serviceMethod string, args interface{}, reply interface{}) error {
conn, err := t.getConn(addr)
if err != nil {
return err
}
err = conn.Call(serviceMethod, args, reply)
conn.lastTime = t.now
checkPersistConnErr(err, conn)
return err
}
// CallWithContext acts like Call but takes a context.
func (t *Transport) CallWithContext(ctx context.Context, addr string, serviceMethod string, args interface{}, reply interface{}) error {
conn, err := t.getConn(addr)
if err != nil {
return err
}
err = conn.CallWithContext(ctx, serviceMethod, args, reply)
conn.lastTime = t.now
checkPersistConnErr(err, conn)
return err
}
// NewStream creates a new Stream for the client side.
func (t *Transport) NewStream(addr, serviceMethod string) (Stream, error) {
conn, err := t.getConn(addr)
if err != nil {
return nil, err
}
stream, err := conn.NewStream(serviceMethod)
conn.lastTime = t.now
checkPersistConnErr(err, conn)
return stream, err
}
// Ping is NOT ICMP ping, this is just used to test whether a connection is still alive.
func (t *Transport) Ping(addr string) error {
conn, err := t.getConn(addr)
if err != nil {
return err
}
err = conn.Ping()
conn.lastTime = t.now
checkPersistConnErr(err, conn)
return err
}
func checkPersistConnErr(err error, pc *persistConn) {
if err == ErrShutdown {
pc.mu.Lock()
pc.alive = false
pc.mu.Unlock()
pc.Close()
}
}
func (t *Transport) getConn(addr string) (pc *persistConn, err error) {
if len(addr) == 0 {
return nil, ErrDial
}
t.connsMu.Lock()
defer t.connsMu.Unlock()
if !t.running {
t.once.Do(func() {
t.idleConns = make(map[string]*connQueue)
t.conns = make(map[string]*conns)
t.done = make(chan bool, 10)
if t.MaxConnsPerHost < 1 {
t.MaxConnsPerHost = MaxConnsPerHost
}
if t.MaxIdleConnsPerHost < 1 {
t.MaxIdleConnsPerHost = MaxIdleConnsPerHost
} else if t.MaxIdleConnsPerHost > t.MaxConnsPerHost {
t.MaxIdleConnsPerHost = t.MaxConnsPerHost
}
if t.KeepAlive <= 0 {
t.KeepAlive = KeepAlive
}
if t.IdleConnTimeout <= 0 {
t.IdleConnTimeout = IdleConnTimeout
}
t.now = time.Now()
if t.Dial == nil {
t.Dial = Dial
}
if t.DialWithOptions == nil {
t.DialWithOptions = DialWithOptions
}
t.running = true
go t.run()
})
}
if cs, ok := t.conns[addr]; ok {
if len(cs.Conns) < t.MaxConnsPerHost {
if cq, ok := t.idleConns[addr]; ok && cq.Length() > 0 {
pc = cq.Dequeue()
pc.lastTime = t.now
pc.mu.Lock()
if !pc.alive {
pc.mu.Unlock()
if pc, err = t.newPersistConn(addr); err != nil {
return nil, err
}
} else {
pc.mu.Unlock()
}
} else if pc, err = t.newPersistConn(addr); err != nil {
return nil, err
}
cs.Append(pc)
t.conns[addr] = cs
return pc, nil
}
cursor := cs.Cursor()
pc = cs.Conns[cursor]
pc.mu.Lock()
if !pc.alive {
pc.mu.Unlock()
if pc, err = t.newPersistConn(addr); err != nil {
return nil, err
}
cs.Conns[cursor] = pc
return
}
pc.mu.Unlock()
return
}
if cq, ok := t.idleConns[addr]; ok && cq.Length() > 0 {
pc = cq.Dequeue()
pc.lastTime = time.Now()
} else {
if pc, err = t.newPersistConn(addr); err != nil {
return nil, err
}
}
cs := &conns{addr: addr}
cs.Append(pc)
t.conns[addr] = cs
return pc, nil
}
func (t *Transport) newPersistConn(addr string) (*persistConn, error) {
var conn *Conn
var err error
if t.Options != nil {
conn, err = t.DialWithOptions(addr, t.Options)
} else {
conn, err = t.Dial(t.Network, addr, t.Codec)
}
if err != nil {
return nil, ErrDial
}
return &persistConn{
Conn: conn,
alive: true,
lastTime: time.Now(),
}, nil
}
func (t *Transport) run() {
if t.ticker <= 0 {
t.ticker = runTicker
}
ticker := time.NewTicker(t.ticker)
for {
select {
case <-ticker.C:
t.now = time.Now()
t.connsMu.Lock()
for _, cs := range t.conns {
length := len(cs.Conns)
for i := 0; i < length; i++ {
pc := cs.Conns[i]
if pc.lastTime.Add(t.KeepAlive).Before(time.Now()) && pc.NumCalls() == 0 {
cs.Delete(i)
i--
length--
if cq, ok := t.idleConns[cs.addr]; ok {
if !t.idleConns[cs.addr].Enqueue(pc) {
pc.Close()
}
} else {
cq = newConnQueue(t.MaxIdleConnsPerHost, cs.addr)
cq.Enqueue(pc)
t.idleConns[cs.addr] = cq
}
} else {
pc.Ping()
}
}
if len(cs.Conns) == 0 {
delete(t.conns, cs.addr)
}
}
for _, cq := range t.idleConns {
length := cq.Length()
for i := 0; i < length; i++ {
if cq.Rear().value.lastTime.Add(t.IdleConnTimeout).Before(time.Now()) {
pc := cq.Dequeue()
pc.Close()
} else {
cq.Rear().value.Ping()
}
}
if cq.Length() == 0 {
delete(t.idleConns, cq.addr)
}
}
t.connsMu.Unlock()
case <-t.done:
ticker.Stop()
return
}
}
}
// CloseIdleConnections closes the idle connections.
func (t *Transport) CloseIdleConnections() {
t.connsMu.Lock()
defer t.connsMu.Unlock()
for _, cs := range t.conns {
length := len(cs.Conns)
for i := 0; i < length; i++ {
pc := cs.Conns[i]
if pc.NumCalls() == 0 {
cs.Delete(i)
i--
length--
pc.Close()
}
}
if len(cs.Conns) == 0 {
delete(t.conns, cs.addr)
}
}
for _, cq := range t.idleConns {
length := cq.Length()
for i := 0; i < length; i++ {
pc := cq.Dequeue()
pc.Close()
}
delete(t.idleConns, cq.addr)
}
}
// Close closes the all connections.
func (t *Transport) Close() error {
if !atomic.CompareAndSwapUint32(&t.closed, 0, 1) {
return nil
}
t.connsMu.Lock()
defer t.connsMu.Unlock()
if !t.running {
return nil
}
for _, cs := range t.conns {
length := len(cs.Conns)
for i := 0; i < length; i++ {
pc := cs.Conns[i]
pc.Close()
}
}
t.conns = make(map[string]*conns)
for _, cq := range t.idleConns {
length := cq.Length()
if length > 0 {
for i := 0; i < length; i++ {
pc := cq.Dequeue()
pc.Close()
}
}
delete(t.idleConns, cq.addr)
}
t.idleConns = make(map[string]*connQueue)
close(t.done)
return nil
}
type persistConn struct {
*Conn
mu sync.Mutex
alive bool
lastTime time.Time
}
type conns struct {
addr string
Conns []*persistConn
cursor int
}
func (c *conns) Cursor() int {
c.cursor++
if c.cursor > len(c.Conns)-1 {
c.cursor = 0
}
return c.cursor
}
func (c *conns) Append(pc *persistConn) {
c.Conns = append(c.Conns, pc)
}
func (c *conns) Delete(cursor int) {
copy(c.Conns[cursor:], c.Conns[cursor+1:])
c.Conns = c.Conns[:len(c.Conns)-1]
}
type node struct {
value *persistConn
previous *node
next *node
}
type connQueue struct {
addr string
front *node
rear *node
length int
capacity int
}
func newConnQueue(capacity int, addr string) *connQueue {
front := &node{
value: nil,
previous: nil,
}
rear := &node{
value: nil,
previous: front,
}
front.next = rear
return &connQueue{
addr: addr,
front: front,
rear: rear,
capacity: capacity,
}
}
func (q *connQueue) Length() int {
return q.length
}
func (q *connQueue) Capacity() int {
return q.capacity
}
func (q *connQueue) Front() *node {
if q.length == 0 {
return nil
}
return q.front.next
}
func (q *connQueue) Rear() *node {
if q.length == 0 {
return nil
}
return q.rear.previous
}
func (q *connQueue) Enqueue(value *persistConn) bool {
if q.length == q.capacity || value == nil {
return false
}
node := &node{
value: value,
}
if q.length == 0 {
q.front.next = node
}
node.previous = q.rear.previous
node.next = q.rear
q.rear.previous.next = node
q.rear.previous = node
q.length++
return true
}
func (q *connQueue) Dequeue() *persistConn {
if q.length == 0 {
return nil
}
result := q.front.next
q.front.next = result.next
result.next = nil
result.previous = nil
q.length--
return result.value
}