Add option to auto-reconnect even on clean disconnects

This commit is contained in:
Tulir Asokan 2021-02-05 19:26:26 +02:00
parent 3c7ff4bc0c
commit 1c801594d5
3 changed files with 11 additions and 4 deletions

View file

@ -42,6 +42,7 @@ type BridgeConfig struct {
MaxConnectionAttempts int `yaml:"max_connection_attempts"` MaxConnectionAttempts int `yaml:"max_connection_attempts"`
ConnectionRetryDelay int `yaml:"connection_retry_delay"` ConnectionRetryDelay int `yaml:"connection_retry_delay"`
ReportConnectionRetry bool `yaml:"report_connection_retry"` ReportConnectionRetry bool `yaml:"report_connection_retry"`
AggressiveReconnect bool `yaml:"aggressive_reconnect"`
ChatListWait int `yaml:"chat_list_wait"` ChatListWait int `yaml:"chat_list_wait"`
PortalSyncWait int `yaml:"portal_sync_wait"` PortalSyncWait int `yaml:"portal_sync_wait"`
UserMessageBuffer int `yaml:"user_message_buffer"` UserMessageBuffer int `yaml:"user_message_buffer"`

View file

@ -106,6 +106,8 @@ bridge:
# Whether or not the bridge should send a notice to the user's management room when it retries connecting. # Whether or not the bridge should send a notice to the user's management room when it retries connecting.
# If false, it will only report when it stops retrying. # If false, it will only report when it stops retrying.
report_connection_retry: true report_connection_retry: true
# Whether or not the bridge should reconnect even if WhatsApp says another web client connected.
aggressive_reconnect: false
# Maximum number of seconds to wait for chats to be sent at startup. # Maximum number of seconds to wait for chats to be sent at startup.
# If this is too low and you have lots of chats, it could cause backfilling to fail. # If this is too low and you have lots of chats, it could cause backfilling to fail.
chat_list_wait: 30 chat_list_wait: 30

12
user.go
View file

@ -746,10 +746,14 @@ func (user *User) HandleError(err error) {
if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok { if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok {
user.bridge.Metrics.TrackDisconnection(user.MXID) user.bridge.Metrics.TrackDisconnection(user.MXID)
if closed.Code == 1000 && user.cleanDisconnection { if closed.Code == 1000 && user.cleanDisconnection {
user.bridge.Metrics.TrackConnectionState(user.JID, false)
user.cleanDisconnection = false user.cleanDisconnection = false
user.log.Infoln("Clean disconnection by server") if !user.bridge.Config.Bridge.AggressiveReconnect {
return user.bridge.Metrics.TrackConnectionState(user.JID, false)
user.log.Infoln("Clean disconnection by server")
return
} else {
user.log.Debugln("Clean disconnection by server, but aggressive reconnection is enabled")
}
} }
go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code)) go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code))
} else if failed, ok := err.(*whatsapp.ErrConnectionFailed); ok { } else if failed, ok := err.(*whatsapp.ErrConnectionFailed); ok {
@ -1084,8 +1088,8 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) {
go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true) go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true)
} }
case whatsappExt.CommandDisconnect: case whatsappExt.CommandDisconnect:
user.cleanDisconnection = true
if cmd.Kind == "replaced" { if cmd.Kind == "replaced" {
user.cleanDisconnection = true
go user.sendMarkdownBridgeAlert("\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" + go user.sendMarkdownBridgeAlert("\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" +
"Use the `reconnect` command to disconnect the other client and resume bridging.") "Use the `reconnect` command to disconnect the other client and resume bridging.")
} else { } else {