Skip to content

Commit

Permalink
Add: gettyRPCClient.safeClose
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Sep 1, 2019
1 parent ce6e685 commit ff4f9b9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions protocol/dubbo/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ func (c *gettyRPCClient) close() error {
return closeErr
}

func (c *gettyRPCClient) safeClose() error {
c.lock.Lock()
defer c.lock.Unlock()

return c.close()
}

type gettyRPCClientPool struct {
rpcClient *Client
size int // size of []*gettyRPCClient
Expand All @@ -270,11 +277,11 @@ func newGettyRPCClientConnPool(rpcClient *Client, size int, ttl time.Duration) *

func (p *gettyRPCClientPool) close() {
p.Lock()
defer p.Unlock()
conns := p.conns
p.conns = nil
p.Unlock()
for _, conn := range conns {
conn.close()
conn.safeClose()
}
}

Expand All @@ -293,7 +300,7 @@ 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.close(); closeErr != nil {
if closeErr := conn.safeClose(); closeErr != nil {
p.remove(conn)
}
continue
Expand All @@ -311,20 +318,20 @@ func (p *gettyRPCClientPool) release(conn *gettyRPCClient, err error) {
return
}

p.Lock()
defer p.Unlock()

if err != nil {
conn.close()
conn.safeClose()
return
}

p.Lock()
defer p.Unlock()

if p.conns == nil {
return
}

if len(p.conns) >= p.size {
if closeErr := conn.close(); closeErr != nil {
if closeErr := conn.safeClose(); closeErr != nil {
// delete @conn from client pool
p.remove(conn)
}
Expand Down

0 comments on commit ff4f9b9

Please sign in to comment.