Retry sending when receiving 599 error

This commit is contained in:
Tulir Asokan 2020-11-04 14:58:24 +02:00
parent a1740db0d7
commit a49a5fa3d4
3 changed files with 19 additions and 7 deletions

2
go.mod
View file

@ -16,4 +16,4 @@ require (
maunium.net/go/mautrix v0.7.13
)
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.3.12
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.3.13

2
go.sum
View file

@ -113,6 +113,8 @@ github.com/tulir/go-whatsapp v0.3.11 h1:z9AvTb8YXYP8t6Y0jium3reshgDpFCg//WNiCGo9
github.com/tulir/go-whatsapp v0.3.11/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o=
github.com/tulir/go-whatsapp v0.3.12 h1:4ovBn1GNMUsCrFR7KdAcByQnkh0o+VoA7Q/wY2UD36A=
github.com/tulir/go-whatsapp v0.3.12/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o=
github.com/tulir/go-whatsapp v0.3.13 h1:RPc/GdZ7KlhlGiZp2Zk7B/OP9v0l7ywOt5I2kKAZ+xU=
github.com/tulir/go-whatsapp v0.3.13/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=

View file

@ -1931,10 +1931,10 @@ func (portal *Portal) wasMessageSent(sender *User, id string) bool {
return true
}
func (portal *Portal) sendErrorMessage(sendErr error) id.EventID {
func (portal *Portal) sendErrorMessage(message string) id.EventID {
resp, err := portal.sendMainIntentMessage(event.MessageEventContent{
MsgType: event.MsgNotice,
Body: fmt.Sprintf("\u26a0 Your message may not have been bridged: %v", sendErr),
Body: fmt.Sprintf("\u26a0 Your message may not have been bridged: %v", message),
})
if err != nil {
portal.log.Warnfln("Failed to send bridging error message:", err)
@ -1966,8 +1966,11 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
return
}
portal.markHandled(sender, info, evt.ID)
portal.log.Debugln("Sending event", evt.ID, "to WhatsApp")
portal.log.Debugln("Sending event", evt.ID, "to WhatsApp", info.Key.GetId())
portal.sendRaw(sender, evt, info, false)
}
func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *waProto.WebMessageInfo, isRetry bool) {
errChan := make(chan error, 1)
go sender.Conn.SendRaw(info, errChan)
@ -1975,13 +1978,20 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
var errorEventID id.EventID
select {
case err = <-errChan:
var statusResp whatsapp.StatusResponse
if !isRetry && errors.As(err, &statusResp) && statusResp.Status == 599 {
portal.log.Debugfln("599 status response sending %s to WhatsApp (%+v), retrying...", evt.ID, statusResp)
errorEventID = portal.sendErrorMessage(fmt.Sprintf("%v. The bridge will retry in 5 seconds.", err))
time.Sleep(5 * time.Second)
portal.sendRaw(sender, evt, info, true)
}
case <-time.After(time.Duration(portal.bridge.Config.Bridge.ConnectionTimeout) * time.Second):
if portal.bridge.Config.Bridge.FetchMessageOnTimeout && portal.wasMessageSent(sender, info.Key.GetId()) {
portal.log.Debugln("Matrix event %s was bridged, but response didn't arrive within timeout")
portal.sendDeliveryReceipt(evt.ID)
} else {
portal.log.Warnfln("Response when bridging Matrix event %s is taking long to arrive", evt.ID)
errorEventID = portal.sendErrorMessage(timeout)
errorEventID = portal.sendErrorMessage(timeout.Error())
}
err = <-errChan
}
@ -1989,9 +1999,9 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err)
var statusResp whatsapp.StatusResponse
if errors.As(err, &statusResp) && statusResp.Status == 599 {
portal.log.Debugln("Extra info in 599 error: %+v", statusResp.Extra)
portal.log.Debugfln("599 status response data: %+v", statusResp)
}
portal.sendErrorMessage(err)
portal.sendErrorMessage(err.Error())
} else {
portal.log.Debugfln("Handled Matrix event %s", evt.ID)
portal.sendDeliveryReceipt(evt.ID)