Skip to content

Commit af9718c

Browse files
committed
Disconnect client only if Slack disconnect is intentional
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 <insomniac@slackware.it>
1 parent 2570c60 commit af9718c

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

event_handler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,19 @@ func eventHandler(ctx *IrcContext, rtm *slack.RTM) {
319319
log.Info("Connected to Slack")
320320
ctx.SlackConnected = true
321321
case *slack.DisconnectedEvent:
322-
log.Warningf("Disconnected from Slack (intentional: %v)", msg.Data.(*slack.DisconnectedEvent).Intentional)
322+
de := msg.Data.(*slack.DisconnectedEvent)
323+
log.Warningf("Disconnected from Slack (intentional: %v, cause: %s)", de.Intentional, de.Cause)
323324
ctx.SlackConnected = false
324-
ctx.Conn.Close()
325+
// only close the IRC client connection if the disconnection from
326+
// Slack was intentional. Otherwise RTM.ManageConnection will retry
327+
// automatically to connect. If reconnection fails, it will surface
328+
// in a subsequent event, so it's safe here not to wait for a
329+
// reconnection.
330+
if de.Intentional {
331+
if err := ctx.Conn.Close(); err != nil {
332+
log.Warningf("Failed to close connection to IRC client: %v", err)
333+
}
334+
}
325335
return
326336
case *slack.MemberJoinedChannelEvent, *slack.MemberLeftChannelEvent:
327337
// refresh the users list

0 commit comments

Comments
 (0)