From fe151e8dd3a5143939e6188779f52ac857300bc6 Mon Sep 17 00:00:00 2001 From: Pavel Gabriel Date: Thu, 7 Sep 2023 16:20:35 +0200 Subject: [PATCH] call ConnectionClosedHandlers when connection is closed by any side (#67) * call ConnectionClosedHandlers when connection is closed by any side * if connection is closed do nothing in reconnect method --- connection.go | 12 ++++++------ pool.go | 10 ++++------ 2 files changed, 10 insertions(+), 12 deletions(-) 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)