Skip to content

Commit

Permalink
Add way to hook into auto-reconnect and cancel it
Browse files Browse the repository at this point in the history
Closes #171
Fixes #170
  • Loading branch information
tulir committed Dec 6, 2023
1 parent 86c8337 commit 1789922
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Client struct {
EnableAutoReconnect bool
LastSuccessfulConnect time.Time
AutoReconnectErrors int
// AutoReconnectHook is called when auto-reconnection fails. If the function returns false,
// the client will not attempt to reconnect. The number of retries can be read from AutoReconnectErrors.
AutoReconnectHook func(error) bool

sendActiveReceipts uint32

Expand Down Expand Up @@ -380,6 +383,10 @@ func (cli *Client) autoReconnect() {
return
} else if err != nil {
cli.Log.Errorf("Error reconnecting after autoreconnect sleep: %v", err)
if cli.AutoReconnectHook != nil && !cli.AutoReconnectHook(err) {
cli.Log.Debugf("AutoReconnectHook returned false, not reconnecting")
return
}
} else {
return
}
Expand Down

0 comments on commit 1789922

Please sign in to comment.