Skip to content

Commit

Permalink
Expose MinFullReconnectInterval in config as `min_full_reconnect_in…
Browse files Browse the repository at this point in the history
…terval_seconds` and DRY'd condition
  • Loading branch information
javiercr committed May 15, 2024
1 parent d9ae865 commit d62b28e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d62b28e

Please sign in to comment.