Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 12795be63b05fe2aeb634262a4b080a2db545bc8 Author: Wim <wim@42.be> Date: Thu Jul 20 23:16:43 2017 +0000 Handle reconnections better (xmpp). Closes #222 diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 0bd664d..504b974 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -46 +47 @@ import (   "crypto/tls"   "github.com/42wim/matterbridge/bridge/config"   log "github.com/Sirupsen/logrus" + "github.com/jpillora/backoff"   "github.com/mattn/go-xmpp"     "strings" @@ -437 +4429 @@ func (b *Bxmpp) Connect() error {   return err   }   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  }   @@ -967 +11911 @@ func (b *Bxmpp) xmppKeepAlive() chan bool {   for {   select {   case <-ticker.C: - b.xc.PingC2S("", "") + flog.Debugf("PING") + err := b.xc.PingC2S("", "") + if err != nil { + flog.Debugf("PING failed %#v", err) + }   case <-done:   return   }