Skip to content
Snippets Groups Projects
Commit a23f85a4 authored by vito.he's avatar vito.he Committed by GitHub
Browse files

Merge pull request #190 from divebomb/develop

Imp:  remove client from pool before closing it
parents 5a86ae83 245c0afc
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,7 @@ type gettyRPCClient struct {
once sync.Once
protocol string
addr string
created int64 // zero, not create or be destroyed
active int64 // zero, not create or be destroyed
pool *gettyRPCClientPool
......@@ -85,11 +85,11 @@ func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*ge
}
func (c *gettyRPCClient) updateActive(active int64) {
atomic.StoreInt64(&c.created, active)
atomic.StoreInt64(&c.active, active)
}
func (c *gettyRPCClient) getActive() int64 {
return atomic.LoadInt64(&c.created)
return atomic.LoadInt64(&c.active)
}
func (c *gettyRPCClient) newSession(session getty.Session) error {
......@@ -154,6 +154,9 @@ func (c *gettyRPCClient) addSession(session getty.Session) {
}
c.lock.Lock()
if c.sessions == nil {
c.sessions = make([]*rpcSession, 0, 16)
}
c.sessions = append(c.sessions, &rpcSession{session: session})
c.lock.Unlock()
}
......@@ -271,7 +274,7 @@ func newGettyRPCClientConnPool(rpcClient *Client, size int, ttl time.Duration) *
rpcClient: rpcClient,
size: size,
ttl: int64(ttl.Seconds()),
conns: []*gettyRPCClient{},
conns: make([]*gettyRPCClient, 0, 16),
}
}
......@@ -300,12 +303,11 @@ func (p *gettyRPCClientPool) getGettyRpcClient(protocol, addr string) (*gettyRPC
p.conns = p.conns[:len(p.conns)-1]
if d := now - conn.getActive(); d > p.ttl {
if closeErr := conn.safeClose(); closeErr == nil {
p.remove(conn)
}
p.remove(conn)
conn.safeClose()
continue
}
conn.updateActive(now) //update created time
conn.updateActive(now) //update active time
return conn, nil
}
......@@ -331,10 +333,9 @@ func (p *gettyRPCClientPool) release(conn *gettyRPCClient, err error) {
}
if len(p.conns) >= p.size {
if closeErr := conn.safeClose(); closeErr == nil {
// delete @conn from client pool
p.remove(conn)
}
// delete @conn from client pool
p.remove(conn)
conn.safeClose()
return
}
p.conns = append(p.conns, 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