diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2020-01-10 16:02:25 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2020-01-10 16:02:25 +0300 |
commit | fd1fd560bc7dedd33a4e5232dfbcd9e0f7d88c92 (patch) | |
tree | 03256d85335fa1722a4719c188d86cba7433f122 /xmpp/gateway/gateway.go | |
parent | 23f2a85301f2ee5af867fa774a406cf308733714 (diff) |
Try to resume connection and resend a stanza on failure
Diffstat (limited to 'xmpp/gateway/gateway.go')
-rw-r--r-- | xmpp/gateway/gateway.go | 28 |
1 files changed, 23 insertions, 5 deletions
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 +} |