From 163f55f9c27e9e8a75774424d22598799e8306c6 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 14 Feb 2017 21:12:02 +0100 Subject: Refactor to handle disconnects/reconnects better. Now try to reconnect every 60 seconds until forever. --- gateway/gateway.go | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'gateway') diff --git a/gateway/gateway.go b/gateway/gateway.go index 82a76ef8..19571776 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -7,6 +7,7 @@ import ( log "github.com/Sirupsen/logrus" "reflect" "strings" + "time" ) type Gateway struct { @@ -39,24 +40,16 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error { } log.Infof("Starting bridge: %s ", cfg.Account) br := bridge.New(gw.Config, cfg, gw.Message) + br.ChannelsOut = gw.ChannelsOut[br.Account] + br.ChannelsIn = gw.ChannelsIn[br.Account] + br.ChannelOptions = gw.ChannelOptions[br.Account] + gw.Bridges[cfg.Account] = br err := br.Connect() if err != nil { return fmt.Errorf("Bridge %s failed to start: %v", br.Account, err) } - exists := make(map[string]bool) - for _, channel := range append(gw.ChannelsOut[br.Account], gw.ChannelsIn[br.Account]...) { - if !exists[br.Account+channel] { - mychannel := channel - log.Infof("%s: joining %s", br.Account, channel) - if br.Protocol == "irc" && gw.ChannelOptions[br.Account+channel].Key != "" { - log.Debugf("using key %s for channel %s", gw.ChannelOptions[br.Account+channel].Key, channel) - mychannel = mychannel + " " + gw.ChannelOptions[br.Account+channel].Key - } - br.JoinChannel(mychannel) - exists[br.Account+channel] = true - } - } + br.JoinChannels() return nil } @@ -76,6 +69,13 @@ func (gw *Gateway) handleReceive() { for { select { case msg := <-gw.Message: + if msg.Event == config.EVENT_FAILURE { + for _, br := range gw.Bridges { + if msg.Account == br.Account { + go gw.reconnectBridge(br) + } + } + } if !gw.ignoreMessage(&msg) { for _, br := range gw.Bridges { gw.handleMessage(msg, br) @@ -85,6 +85,20 @@ func (gw *Gateway) handleReceive() { } } +func (gw *Gateway) reconnectBridge(br *bridge.Bridge) { + br.Disconnect() + time.Sleep(time.Second * 5) +RECONNECT: + log.Infof("Reconnecting %s", br.Account) + err := br.Connect() + if err != nil { + log.Errorf("Reconnection failed: %s. Trying again in 60 seconds", err) + time.Sleep(time.Second * 60) + goto RECONNECT + } + br.JoinChannels() +} + func (gw *Gateway) mapChannels() error { options := make(map[string]config.ChannelOptions) m := make(map[string][]string) -- cgit v1.2.3