Skip to content

Commit

Permalink
Disconnect client only if Slack disconnect is intentional
Browse files Browse the repository at this point in the history
If the disconnection from Slack is intentiona, just keep closing the IRC
client connection as previously. However, if the disconnection is not
intentional, it's not necessary to disconnect the IRC client, as the
Slack reconnection will be handled by Slack's RTM.ManageConnection.
This should reduce the amount of disconnect/reconnect on the IRC client
side.

Signed-off-by: Andrea Barberio <[email protected]>
  • Loading branch information
insomniacslk committed May 2, 2020
1 parent 2570c60 commit af9718c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,19 @@ func eventHandler(ctx *IrcContext, rtm *slack.RTM) {
log.Info("Connected to Slack")
ctx.SlackConnected = true
case *slack.DisconnectedEvent:
log.Warningf("Disconnected from Slack (intentional: %v)", msg.Data.(*slack.DisconnectedEvent).Intentional)
de := msg.Data.(*slack.DisconnectedEvent)
log.Warningf("Disconnected from Slack (intentional: %v, cause: %s)", de.Intentional, de.Cause)
ctx.SlackConnected = false
ctx.Conn.Close()
// only close the IRC client connection if the disconnection from
// Slack was intentional. Otherwise RTM.ManageConnection will retry
// automatically to connect. If reconnection fails, it will surface
// in a subsequent event, so it's safe here not to wait for a
// reconnection.
if de.Intentional {
if err := ctx.Conn.Close(); err != nil {
log.Warningf("Failed to close connection to IRC client: %v", err)
}
}
return
case *slack.MemberJoinedChannelEvent, *slack.MemberLeftChannelEvent:
// refresh the users list
Expand Down

0 comments on commit af9718c

Please sign in to comment.