diff --git a/connection.go b/connection.go index 673ffc7..78bc797 100644 --- a/connection.go +++ b/connection.go @@ -259,12 +259,6 @@ func (c *Connection) handleConnectionError(err error) { // close everything else we close normally c.close() - - if c.Opts.ConnectionClosedHandlers != nil && len(c.Opts.ConnectionClosedHandlers) > 0 { - for _, handler := range c.Opts.ConnectionClosedHandlers { - go handler(c) - } - } } func (c *Connection) close() error { @@ -280,6 +274,12 @@ func (c *Connection) close() error { } } + if c.Opts.ConnectionClosedHandlers != nil && len(c.Opts.ConnectionClosedHandlers) > 0 { + for _, handler := range c.Opts.ConnectionClosedHandlers { + go handler(c) + } + } + return nil } diff --git a/pool.go b/pool.go index 5ca9a2c..211c094 100644 --- a/pool.go +++ b/pool.go @@ -161,6 +161,10 @@ func (p *Pool) handleClosedConnection(closedConn *Connection) { p.mu.Lock() defer p.mu.Unlock() + if p.isClosed { + return + } + var connIndex = -1 for i, conn := range p.connections { if conn == closedConn { @@ -181,12 +185,6 @@ func (p *Pool) handleClosedConnection(closedConn *Connection) { p.connections[connsNum-1] = nil // Erase last element p.connections = p.connections[:connsNum-1] // Truncate slice. - // if pool was closed, don't start recreate goroutine - // should we return earlier and keep connection in the pool as it will be closed anyway? - if p.isClosed { - return - } - // initiate goroutine to reconnect to closedConn.Addr p.wg.Add(1) go p.recreateConnection(closedConn)