Skip to content

Commit

Permalink
Retry a few times before sending transient disconnect states
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed May 3, 2024
1 parent 19cb048 commit 286a82c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (c *Client) Connect() error {
(*oldCancel)()
}
go func() {
connectionAttempts := 1
reconnectIn := 2 * time.Second
for {
connectStart := time.Now()
Expand All @@ -231,7 +232,8 @@ func (c *Client) Connect() error {
c.eventHandler(&Event_PermanentError{Err: err})
return
}
c.eventHandler(&Event_SocketError{Err: err})
connectionAttempts += 1
c.eventHandler(&Event_SocketError{Err: err, ConnectionAttempts: connectionAttempts})
if time.Since(connectStart) > 2*time.Minute {
reconnectIn = 2 * time.Second
} else {
Expand Down
5 changes: 4 additions & 1 deletion messagix/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ type Event_PingResp struct{}
func (pr *Event_PingResp) SetIdentifier(identifier uint16) {}
func (e *Event_PingResp) Finish() ResponseData { return e }

type Event_SocketError struct{ Err error }
type Event_SocketError struct {
Err error
ConnectionAttempts int
}

type Event_PermanentError struct{ Err error }

Expand Down
5 changes: 4 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
)

const MinFullReconnectInterval = 1 * time.Hour
const setDisconnectStateAfterConnectAttempts = 3

func (br *MetaBridge) GetUserByMXID(userID id.UserID) *User {
return br.maybeGetUserByMXID(userID, &userID)
Expand Down Expand Up @@ -1113,7 +1114,9 @@ func (user *User) eventHandler(rawEvt any) {
StateEvent: status.StateTransientDisconnect,
Error: MetaTransientDisconnect,
}
user.BridgeState.Send(user.metaState)
if evt.ConnectionAttempts > setDisconnectStateAfterConnectAttempts {
user.BridgeState.Send(user.metaState)
}
case *messagix.Event_Reconnected:
user.log.Debug().Msg("Reconnected to Meta socket")
user.metaState = status.BridgeState{StateEvent: status.StateConnected}
Expand Down

0 comments on commit 286a82c

Please sign in to comment.