Improve handling of errors noticed in provisioning API pings

This commit is contained in:
Tulir Asokan 2021-02-24 00:06:11 +02:00
parent a5d60bf87d
commit 69e5a26e01
4 changed files with 17 additions and 2 deletions

2
go.mod
View file

@ -16,4 +16,4 @@ require (
maunium.net/go/mautrix v0.8.3
)
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b

2
go.sum
View file

@ -297,6 +297,8 @@ github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223140805-2de4c4c5473f h1:7GPYeq
github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223140805-2de4c4c5473f/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E=
github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8 h1:WVZyCGqywD7ss+soR35mBPim3OWrgaJTjqQHEvhoJRg=
github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E=
github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b h1:oAcxqLVVaT5nzeB7P/Orlp4h4e9WaphcBUSTJgK0fDo=
github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=

View file

@ -268,6 +268,12 @@ func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) {
user.log.Debugln("Pinging WhatsApp mobile due to /ping API request")
err := user.Conn.AdminTest()
var errStr string
if err == whatsapp.ErrPingFalse {
user.log.Debugln("Forwarding ping false error from provisioning API to HandleError")
go user.HandleError(err)
} else if errors.Is(err, whatsapp.ErrConnectionTimeout) {
user.Conn.CountTimeout()
}
if err != nil {
errStr = err.Error()
}

View file

@ -284,7 +284,7 @@ func (user *User) DeleteConnection() {
func (user *User) RestoreSession() bool {
if user.Session != nil {
user.Conn.SetSession(*user.Session)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
err := user.Conn.Restore(true, ctx)
if err == whatsapp.ErrAlreadyLoggedIn {
@ -841,6 +841,10 @@ func (user *User) HandleError(err error) {
user.ConnectionErrors++
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", failed.Err))
} else if err == whatsapp.ErrPingFalse {
disconnectErr := user.Conn.Disconnect()
if disconnectErr != nil {
user.log.Warnln("Failed to disconnect after failed ping:", disconnectErr)
}
user.bridge.Metrics.TrackDisconnection(user.MXID)
user.ConnectionErrors++
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", err))
@ -902,6 +906,9 @@ func (user *User) tryReconnect(msg string) {
user.sendMarkdownBridgeAlert("\u26a0 Failed to reconnect to WhatsApp: unpaired from phone. " +
"To re-pair your phone, log in again.")
return
} else if errors.Is(err, whatsapp.ErrAlreadyLoggedIn) {
user.log.Warnln("Reconnection said we're already logged in, not trying anymore")
return
} else {
user.log.Errorln("Error while trying to reconnect after disconnection:", err)
}