From af9718c146d60dd7c98bd70dbba0b103949a543a Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Sat, 2 May 2020 15:04:16 +0100 Subject: [PATCH] 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 --- event_handler.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/event_handler.go b/event_handler.go index 2403b07..47cc294 100644 --- a/event_handler.go +++ b/event_handler.go @@ -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