diff --git a/remoting/getty/pool.go b/remoting/getty/pool.go index aaa3bbd70d1fea16374a1f368a95fdc9baf85057..fdefa238202f0b650c9b7cf3389b81cca2f1c80d 100644 --- a/remoting/getty/pool.go +++ b/remoting/getty/pool.go @@ -98,7 +98,6 @@ func (c *gettyRPCClient) newSession(session getty.Session) error { tcpConn *net.TCPConn conf ClientConfig ) - conf = c.pool.rpcClient.conf if conf.GettySessionParam.CompressEncoding { session.SetCompressType(getty.CompressZip) @@ -301,7 +300,8 @@ func newGettyRPCClientConnPool(rpcClient *Client, size int, ttl time.Duration) * rpcClient: rpcClient, size: size, ttl: int64(ttl.Seconds()), - conns: make([]*gettyRPCClient, 0, 16), + // init capacity : 2 + conns: make([]*gettyRPCClient, 0, 2), } } @@ -320,6 +320,9 @@ func (p *gettyRPCClientPool) getGettyRpcClient(addr string) (*gettyRPCClient, er if err == nil && conn == nil { // create new conn rpcClientConn, err := newGettyRPCClientConn(p, addr) + if err == nil { + p.put(rpcClientConn) + } return rpcClientConn, perrors.WithStack(err) } return conn, perrors.WithStack(err) @@ -333,10 +336,15 @@ func (p *gettyRPCClientPool) get() (*gettyRPCClient, error) { if p.conns == nil { return nil, errClientPoolClosed } - - for len(p.conns) > 0 { - conn := p.conns[len(p.conns)-1] - p.conns = p.conns[:len(p.conns)-1] + for num := len(p.conns); num > 0; { + var conn *gettyRPCClient + if num != 1 { + conn = p.conns[rand.Int31n(int32(num))] + } else { + conn = p.conns[0] + } + // This will recreate gettyRpcClient for remove last position + //p.conns = p.conns[:len(p.conns)-1] if d := now - conn.getActive(); d > p.ttl { p.remove(conn) @@ -353,21 +361,17 @@ func (p *gettyRPCClientPool) put(conn *gettyRPCClient) { if conn == nil || conn.getActive() == 0 { return } - p.Lock() defer p.Unlock() - if p.conns == nil { return } - // check whether @conn has existed in p.conns or not. for i := range p.conns { if p.conns[i] == conn { return } } - if len(p.conns) >= p.size { // delete @conn from client pool // p.remove(conn)