From 9d6c73eb9d9ca5f3d2dc31177122bd4932420b72 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 3 Jun 2015 14:18:15 +0300 Subject: [PATCH] Don't panic if list is closed and conn can't be found. --- pool.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pool.go b/pool.go index 16efc598..090270b5 100644 --- a/pool.go +++ b/pool.go @@ -80,6 +80,9 @@ func (l *connList) Remove(cn *conn) error { } } + if l.closed() { + return nil + } panic("conn not found in the list") } @@ -94,6 +97,9 @@ func (l *connList) Replace(cn, newcn *conn) error { } } + if l.closed() { + return newcn.Close() + } panic("conn not found in the list") } @@ -110,6 +116,10 @@ func (l *connList) Close() (retErr error) { return retErr } +func (l *connList) closed() bool { + return l.cns == nil +} + type connPool struct { dialer func() (*conn, error) @@ -245,11 +255,6 @@ func (p *connPool) Put(cn *conn) error { } func (p *connPool) Remove(cn *conn) error { - if p.closed() { - // Close already closed all connections. - return nil - } - // Replace existing connection with new one and unblock waiter. newcn, err := p.new() if err != nil { @@ -372,12 +377,6 @@ func (p *singleConnPool) Put(cn *conn) error { return nil } -func (p *singleConnPool) put() error { - err := p.pool.Put(p.cn) - p.cn = nil - return err -} - func (p *singleConnPool) Remove(cn *conn) error { defer p.cnMtx.Unlock() p.cnMtx.Lock() @@ -427,7 +426,8 @@ func (p *singleConnPool) Close() error { var err error if p.cn != nil { if p.reusable { - err = p.put() + err = p.pool.Put(p.cn) + p.cn = nil } else { err = p.remove() }