Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry a few times before sending transient disconnect states #56

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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