Handle reconnections better (xmpp). Closes #222

This commit is contained in:
Wim 2017-07-20 23:16:43 +02:00
parent 58779e0d65
commit 0f791d7a9a

View File

@ -4,6 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/jpillora/backoff"
"github.com/mattn/go-xmpp" "github.com/mattn/go-xmpp"
"strings" "strings"
@ -43,7 +44,29 @@ func (b *Bxmpp) Connect() error {
return err return err
} }
flog.Info("Connection succeeded") flog.Info("Connection succeeded")
go b.handleXmpp() go func() {
initial := true
bf := &backoff.Backoff{
Min: time.Second,
Max: 5 * time.Minute,
Jitter: true,
}
for {
if initial {
b.handleXmpp()
initial = false
}
d := bf.Duration()
flog.Infof("Disconnected. Reconnecting in %s", d)
time.Sleep(d)
b.xc, err = b.createXMPP()
if err == nil {
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: "", Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
b.handleXmpp()
bf.Reset()
}
}
}()
return nil return nil
} }
@ -96,7 +119,11 @@ func (b *Bxmpp) xmppKeepAlive() chan bool {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
b.xc.PingC2S("", "") flog.Debugf("PING")
err := b.xc.PingC2S("", "")
if err != nil {
flog.Debugf("PING failed %#v", err)
}
case <-done: case <-done:
return return
} }