@@ -229,7 +229,7 @@ func (c *gettyRPCClient) removeSession(session getty.Session) {
229
229
}
230
230
}()
231
231
if removeFlag {
232
- c .pool .safeRemove ( c )
232
+ c .pool .resetConn ( )
233
233
c .close ()
234
234
}
235
235
}
@@ -325,123 +325,58 @@ func (c *gettyRPCClient) close() error {
325
325
326
326
type gettyRPCClientPool struct {
327
327
rpcClient * Client
328
- size int // size of []*gettyRPCClient
329
- ttl int64 // ttl of every gettyRPCClient, it is checked when getConn
330
328
sslEnabled bool
329
+ closed bool
331
330
332
331
sync.Mutex
333
- conns [] * gettyRPCClient
332
+ conn * gettyRPCClient
334
333
}
335
334
336
- func newGettyRPCClientConnPool (rpcClient * Client , size int , ttl time. Duration ) * gettyRPCClientPool {
335
+ func newGettyRPCClientConnPool (rpcClient * Client ) * gettyRPCClientPool {
337
336
return & gettyRPCClientPool {
338
337
rpcClient : rpcClient ,
339
- size : size ,
340
- ttl : int64 (ttl .Seconds ()),
341
- // init capacity : 2
342
- conns : make ([]* gettyRPCClient , 0 , 2 ),
338
+ closed : false ,
343
339
}
344
340
}
345
341
346
342
func (p * gettyRPCClientPool ) close () {
347
343
p .Lock ()
348
- conns := p .conns
349
- p .conns = nil
344
+ conn := p .conn
345
+ p .conn = nil
346
+ p .closed = true
350
347
p .Unlock ()
351
- for _ , conn := range conns {
348
+ if conn != nil {
352
349
conn .close ()
353
350
}
354
351
}
355
352
356
353
func (p * gettyRPCClientPool ) getGettyRpcClient (addr string ) (* gettyRPCClient , error ) {
354
+ p .Lock ()
355
+ defer p .Unlock ()
357
356
conn , connErr := p .get ()
358
357
if connErr == nil && conn == nil {
359
358
// create new conn
360
359
rpcClientConn , rpcErr := newGettyRPCClientConn (p , addr )
361
360
if rpcErr == nil {
362
- p .put ( rpcClientConn )
361
+ p .conn = rpcClientConn
363
362
}
364
363
return rpcClientConn , perrors .WithStack (rpcErr )
365
364
}
366
365
return conn , perrors .WithStack (connErr )
367
366
}
368
367
369
368
func (p * gettyRPCClientPool ) get () (* gettyRPCClient , error ) {
370
- now := time .Now ().Unix ()
371
-
372
- p .Lock ()
373
- defer p .Unlock ()
374
- if p .conns == nil {
369
+ if p .closed {
375
370
return nil , errClientPoolClosed
376
371
}
377
- for num := len (p .conns ); num > 0 ; {
378
- var conn * gettyRPCClient
379
- if num != 1 {
380
- conn = p .conns [rand .Int31n (int32 (num ))]
381
- } else {
382
- conn = p .conns [0 ]
383
- }
384
- // This will recreate gettyRpcClient for remove last position
385
- // p.conns = p.conns[:len(p.conns)-1]
386
-
387
- if d := now - conn .getActive (); d > p .ttl {
388
- p .remove (conn )
389
- go conn .close ()
390
- num = len (p .conns )
391
- continue
392
- }
393
- conn .updateActive (now ) // update active time
394
- return conn , nil
372
+ if p .conn != nil {
373
+ return p .conn , nil
395
374
}
396
375
return nil , nil
397
376
}
398
377
399
- func (p * gettyRPCClientPool ) put (conn * gettyRPCClient ) {
400
- if conn == nil || conn .getActive () == 0 {
401
- return
402
- }
403
- p .Lock ()
404
- defer p .Unlock ()
405
- if p .conns == nil {
406
- return
407
- }
408
- // check whether @conn has existed in p.conns or not.
409
- for i := range p .conns {
410
- if p .conns [i ] == conn {
411
- return
412
- }
413
- }
414
- if len (p .conns ) >= p .size {
415
- // delete @conn from client pool
416
- // p.remove(conn)
417
- conn .close ()
418
- return
419
- }
420
- p .conns = append (p .conns , conn )
421
- }
422
-
423
- func (p * gettyRPCClientPool ) remove (conn * gettyRPCClient ) {
424
- if conn == nil || conn .getActive () == 0 {
425
- return
426
- }
427
-
428
- if p .conns == nil {
429
- return
430
- }
431
-
432
- if len (p .conns ) > 0 {
433
- for idx , c := range p .conns {
434
- if conn == c {
435
- p .conns = append (p .conns [:idx ], p .conns [idx + 1 :]... )
436
- break
437
- }
438
- }
439
- }
440
- }
441
-
442
- func (p * gettyRPCClientPool ) safeRemove (conn * gettyRPCClient ) {
378
+ func (p * gettyRPCClientPool ) resetConn () {
443
379
p .Lock ()
444
380
defer p .Unlock ()
445
-
446
- p .remove (conn )
381
+ p .conn = nil
447
382
}
0 commit comments