Try to resume connection and resend a stanza on failure

This commit is contained in:
bodqhrohro 2020-01-10 15:02:25 +02:00
parent 23f2a85301
commit fd1fd560bc
3 changed files with 39 additions and 9 deletions

View file

@ -76,9 +76,9 @@ func heartbeat(component *xmpp.Component) {
for { for {
time.Sleep(60e9) time.Sleep(60e9)
for key, presence := range gateway.Queue { for key, presence := range gateway.Queue {
err = component.Send(presence) err = gateway.ResumableSend(component, presence)
if err != nil { if err != nil {
gateway.LogBadPresence(err, presence) gateway.LogBadPresence(presence)
} else { } else {
delete(gateway.Queue, key) delete(gateway.Queue, key)
} }

View file

@ -2,6 +2,7 @@ package gateway
import ( import (
"encoding/xml" "encoding/xml"
"strings"
"dev.narayana.im/narayana/telegabber/xmpp/extensions" "dev.narayana.im/narayana/telegabber/xmpp/extensions"
@ -55,12 +56,12 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
} }
} }
_ = component.Send(message) _ = ResumableSend(component, message)
} }
// LogBadPresence verbosely logs a presence // LogBadPresence verbosely logs a presence
func LogBadPresence(err error, presence *stanza.Presence) { func LogBadPresence(presence *stanza.Presence) {
log.Errorf("Couldn't send presence: %v: %#v", err, presence) log.Errorf("Couldn't send presence: %#v", presence)
} }
// SPFrom is a Telegram user id // SPFrom is a Telegram user id
@ -167,9 +168,9 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error {
immed := SPImmed.Get(args) immed := SPImmed.Get(args)
if immed { if immed {
err := component.Send(presence) err := ResumableSend(component, presence)
if err != nil { if err != nil {
LogBadPresence(err, &presence) LogBadPresence(&presence)
return err return err
} }
} else { } else {
@ -178,3 +179,20 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error {
return nil return nil
} }
// ResumableSend tries to resume the connection once and sends the packet again
func ResumableSend(component *xmpp.Component, packet stanza.Packet) error {
err := component.Send(packet)
if err != nil && strings.HasPrefix(err.Error(), "cannot send packet") {
log.Warn("Packet send failed, trying to resume the connection...")
err = component.Connect()
if err == nil {
err = component.Send(packet)
}
}
if err != nil {
log.Error(err.Error())
}
return err
}

View file

@ -126,7 +126,13 @@ func handleSubscription(s xmpp.Sender, p stanza.Presence) {
Type: "subscribed", Type: "subscribed",
}} }}
_ = s.Send(reply) component, ok := s.(*xmpp.Component)
if !ok {
log.Error("Not a component")
return
}
_ = gateway.ResumableSend(component, reply)
} }
func handlePresence(s xmpp.Sender, p stanza.Presence) { func handlePresence(s xmpp.Sender, p stanza.Presence) {
@ -262,5 +268,11 @@ func handleGetVcardTempIq(s xmpp.Sender, iq stanza.IQ) {
} }
log.Debugf("%#v", answer) log.Debugf("%#v", answer)
_ = s.Send(answer) component, ok := s.(*xmpp.Component)
if !ok {
log.Error("Not a component")
return
}
_ = gateway.ResumableSend(component, answer)
} }