commit 42d2e8ba0eedf93aa4439b46a52b66f65523fad0
Author: Wim <wim@42.be>
Date: Sun Jun 30 18:34:38 2019 +0000
diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go
index 2376d60..e950bcd 100644
--- a/bridge/xmpp/xmpp.go
+++ b/bridge/xmpp/xmpp.go
@@ -27 +29 @@ package bxmpp
import (
"crypto/tls"
+ "fmt"
"strings"
+ "sync"
"time"
"github.com/42wim/matterbridge/bridge"
@@ -196 +218 @@ type Bxmpp struct {
startTime time.Time
xc *xmpp.Client
xmppMap map[string]string
+ connected bool
+ sync.RWMutex
}
func New(cfg *bridge.Config) bridge.Bridger {
@@ -556 +5910 @@ func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error {
}
func (b *Bxmpp) Send(msg config.Message) (string, error) {
+ // should be fixed by using a cache instead of dropping
+ if !b.Connected() {
+ return "", fmt.Errorf("bridge %s not connected, dropping message %#v to bridge", b.Account, msg)
+ }
// ignore delete messages
if msg.Event == config.EventMsgDelete {
return "", nil
@@ -1246 +1327 @@ func (b *Bxmpp) createXMPP() error {
}
func (b *Bxmpp) manageConnection() {
+ b.setConnected(true)
initial := true
bf := &backoff.Backoff{
Min: time.Second,
@@ -1486 +1577 @@ func (b *Bxmpp) manageConnection() {
if err := b.handleXMPP(); err != nil {
b.Log.WithError(err).Error("Disconnected.")
+ b.setConnected(false)
}
// Reconnection loop using an exponential back-off strategy. We
@@ -1596 +1697 @@ func (b *Bxmpp) manageConnection() {
b.Log.Infof("Reconnecting now.")
if err := b.createXMPP(); err == nil {
+ b.setConnected(true)
bf.Reset()
break
}
@@ -3343 +34515 @@ func (b *Bxmpp) skipMessage(message xmpp.Chat) bool {
// skip delayed messages
return !message.Stamp.IsZero() && time.Since(message.Stamp).Minutes() > 5
}
+
+func (b *Bxmpp) setConnected(state bool) {
+ b.Lock()
+ b.connected = state
+ defer b.Unlock()
+}
+
+func (b *Bxmpp) Connected() bool {
+ b.RLock()
+ defer b.RUnlock()
+ return b.connected
+}