Browse Source

Add option to auto-reconnect even on clean disconnects

Tulir Asokan 4 years ago
parent
commit
1c801594d5
3 changed files with 11 additions and 4 deletions
  1. 1 0
      config/bridge.go
  2. 2 0
      example-config.yaml
  3. 8 4
      user.go

+ 1 - 0
config/bridge.go

@@ -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"`

+ 2 - 0
example-config.yaml

@@ -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

+ 8 - 4
user.go

@@ -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")
-			return
+			if !user.bridge.Config.Bridge.AggressiveReconnect {
+				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 {