4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-06-26 07:29:24 +00:00

Refactor to handle disconnects/reconnects better.

Now try to reconnect every 60 seconds until forever.
This commit is contained in:
Wim
2017-02-14 21:12:02 +01:00
parent 2d16fd085e
commit 163f55f9c2
11 changed files with 99 additions and 24 deletions

View File

@ -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)