From d62b28ec5282e074e36f8b619208640f6db6cac3 Mon Sep 17 00:00:00 2001 From: Javier Cuevas Date: Wed, 15 May 2024 13:46:23 +0200 Subject: [PATCH] Expose `MinFullReconnectInterval` in config as `min_full_reconnect_interval_seconds` and DRY'd condition --- config/config.go | 10 +++++----- example-config.yaml | 2 ++ portal.go | 2 +- user.go | 13 ++++++++----- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 91dfab4..a911939 100644 --- a/config/config.go +++ b/config/config.go @@ -46,11 +46,11 @@ type Config struct { *bridgeconfig.BaseConfig `yaml:",inline"` Meta struct { - Mode BridgeMode `yaml:"mode"` - IGE2EE bool `yaml:"ig_e2ee"` - Proxy string `yaml:"proxy"` - GetProxyFrom string `yaml:"get_proxy_from"` - RefreshIntervalSeconds uint64 `yaml:"refresh_interval_seconds"` + Mode BridgeMode `yaml:"mode"` + IGE2EE bool `yaml:"ig_e2ee"` + Proxy string `yaml:"proxy"` + GetProxyFrom string `yaml:"get_proxy_from"` + MinFullReconnectIntervalSeconds int `yaml:"min_full_reconnect_interval_seconds"` } `yaml:"meta"` Bridge BridgeConfig `yaml:"bridge"` diff --git a/example-config.yaml b/example-config.yaml index ed9d504..ee5fce1 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -97,6 +97,8 @@ meta: get_proxy_from: # Interval to force refresh the connection (disconnect and re-connect), default is 1 day. Set 0 to disable refreshes. refresh_interval_seconds: 86400 + # Minimum interval between full reconnects in seconds + min_full_reconnect_interval_seconds: 1 # Bridge config bridge: diff --git a/portal.go b/portal.go index 3334fb8..a556f8b 100644 --- a/portal.go +++ b/portal.go @@ -609,7 +609,7 @@ func (portal *Portal) handleMatrixMessage(ctx context.Context, sender *User, evt } else { ctx = context.WithValue(ctx, msgconvContextKeyClient, sender.Client) tasks, otid, err = portal.MsgConv.ToMeta(ctx, evt, content, relaybotFormatted) - if errors.Is(err, metaTypes.ErrPleaseReloadPage) && time.Since(sender.lastFullReconnect) > MinFullReconnectInterval { + if errors.Is(err, metaTypes.ErrPleaseReloadPage) && sender.canReconnect() { log.Err(err).Msg("Got please reload page error while converting message, reloading page in background") go sender.FullReconnect() err = errReloading diff --git a/user.go b/user.go index 0f952f9..7ec3996 100644 --- a/user.go +++ b/user.go @@ -60,7 +60,6 @@ var ( ErrNotLoggedIn = errors.New("not logged in") ) -const MinFullReconnectInterval = 1 * time.Hour const setDisconnectStateAfterConnectAttempts = 3 func (br *MetaBridge) GetUserByMXID(userID id.UserID) *User { @@ -1027,7 +1026,7 @@ func (user *User) e2eeEventHandler(rawEvt any) { } user.BridgeState.Send(user.waState) case *events.CATRefreshError: - if errors.Is(evt.Error, types.ErrPleaseReloadPage) && time.Since(user.lastFullReconnect) > MinFullReconnectInterval { + if errors.Is(evt.Error, types.ErrPleaseReloadPage) && user.canReconnect() { user.log.Err(evt.Error).Msg("Got CATRefreshError, reloading page") go user.FullReconnect() return @@ -1041,7 +1040,7 @@ func (user *User) e2eeEventHandler(rawEvt any) { go user.sendMarkdownBridgeAlert(context.TODO(), "Error in WhatsApp connection: %s", evt.PermanentDisconnectDescription()) case events.PermanentDisconnect: cf, ok := evt.(*events.ConnectFailure) - if ok && cf.Reason == events.ConnectFailureLoggedOut && time.Since(user.lastFullReconnect) > MinFullReconnectInterval { + if ok && cf.Reason == events.ConnectFailureLoggedOut && user.canReconnect() { user.log.Debug().Msg("Doing full reconnect after WhatsApp 401 error") go user.FullReconnect() } @@ -1146,7 +1145,7 @@ func (user *User) eventHandler(rawEvt any) { StateEvent: status.StateUnknownError, Error: MetaServerUnavailable, } - if time.Since(user.lastFullReconnect) > MinFullReconnectInterval { + if user.canReconnect() { user.log.Debug().Msg("Doing full reconnect after server unavailable error") go user.FullReconnect() } @@ -1196,10 +1195,14 @@ func (user *User) unlockedDisconnect() { user.metaState = status.BridgeState{} } +func (user *User) canReconnect() bool { + return time.Since(user.lastFullReconnect) > time.Duration(user.bridge.Config.Meta.MinFullReconnectIntervalSeconds)*time.Second +} + func (user *User) FullReconnect() { user.Lock() defer user.Unlock() - if time.Since(user.lastFullReconnect) < MinFullReconnectInterval { + if !user.canReconnect() { return } user.unlockedDisconnect()