Skip to content

Commit

Permalink
Add retry loop for initial connection (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Tulir Asokan <tulir@maunium.net>
  • Loading branch information
Fizzadar and tulir authored Feb 16, 2024
1 parent 0723c76 commit 9e90376
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import (
"go.mau.fi/mautrix-meta/messagix/types"
)

const maxConnectAttempts = 5

var (
ErrNotConnected = errors.New("not connected")
ErrNotLoggedIn = errors.New("not logged in")
Expand Down Expand Up @@ -469,6 +471,19 @@ func (user *User) GetMXID() id.UserID {

var MessagixPlatform types.Platform

func isNotNetworkError(err error) bool {
if errors.Is(err, messagix.ErrTokenInvalidated) ||
errors.Is(err, messagix.ErrChallengeRequired) ||
errors.Is(err, messagix.ErrConsentRequired) {
return true
}
lsErr := &messagix.LSErrorResponse{}
if errors.As(err, &lsErr) {
return true
}
return false
}

func (user *User) Connect() {
user.Lock()
defer user.Unlock()
Expand Down Expand Up @@ -572,10 +587,20 @@ func (user *User) unlockedConnectWithCookies(cookies cookies.Cookies) (*messagix
}
user.log.Debug().Msg("Connecting to Meta")
// TODO set proxy for media client?
cli, err := messagix.NewClient(MessagixPlatform, cookies, log, proxyAddr)
if err != nil {
return nil, fmt.Errorf("failed to prepare client: %w", err)
attempts := 1
var cli *messagix.Client
for {
cli, err = messagix.NewClient(MessagixPlatform, cookies, log, proxyAddr)
if err != nil {
if attempts > maxConnectAttempts || isNotNetworkError(err) {
return nil, fmt.Errorf("failed to prepare client: %w", err)
}
attempts += 1
continue
}
break
}

if user.bridge.Config.Meta.GetProxyFrom != "" {
cli.GetNewProxy = user.getProxy
}
Expand Down

0 comments on commit 9e90376

Please sign in to comment.