Skip to content

Commit

Permalink
call ConnectionClosedHandlers when connection is closed by any side (#67
Browse files Browse the repository at this point in the history
)

* call ConnectionClosedHandlers when connection is closed by any side

* if connection is closed do nothing in reconnect method
  • Loading branch information
alovak authored Sep 7, 2023
1 parent 685c6fd commit fe151e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 6 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
10 changes: 4 additions & 6 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit fe151e8

Please sign in to comment.