Skip to content
Snippets Groups Projects
Commit fe3c5b04 authored by cvictory's avatar cvictory
Browse files

fix issue: create gettyClientConn every time

parent dc2b68f2
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment