From fd1fd560bc7dedd33a4e5232dfbcd9e0f7d88c92 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Fri, 10 Jan 2020 15:02:25 +0200 Subject: Try to resume connection and resend a stanza on failure --- xmpp/gateway/gateway.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'xmpp/gateway/gateway.go') diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go index 8629698..01269d0 100644 --- a/xmpp/gateway/gateway.go +++ b/xmpp/gateway/gateway.go @@ -2,6 +2,7 @@ package gateway import ( "encoding/xml" + "strings" "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 -func LogBadPresence(err error, presence *stanza.Presence) { - log.Errorf("Couldn't send presence: %v: %#v", err, presence) +func LogBadPresence(presence *stanza.Presence) { + log.Errorf("Couldn't send presence: %#v", presence) } // 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) if immed { - err := component.Send(presence) + err := ResumableSend(component, presence) if err != nil { - LogBadPresence(err, &presence) + LogBadPresence(&presence) return err } } else { @@ -178,3 +179,20 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error { 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 +} -- cgit v1.2.3