Skip to content

Commit

Permalink
improve exp backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 21, 2022
1 parent b6b8a3e commit 8feef4b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 18 additions & 0 deletions pkg/wrapper/exponentialBackoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ func NewExponentialBackoff(ctx context.Context, max int) *ExponentialBackoff {
return e
}

// LoopForever execute the callback with exponential backoff
// The callback return true if we should continue retrying
// or false if we should stop and exit.
func (e *ExponentialBackoff) LoopForever(clb func() bool) {
for {
keepLooping := clb()
if !keepLooping {
return
}
e.Wait()
select {
case <-e.ctx.Done():
return
default:
}
}
}

// Wait ...
func (e *ExponentialBackoff) Wait() {
if e.val == 0 {
Expand Down
9 changes: 4 additions & 5 deletions pkg/wrapper/ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,15 @@ func (b *OGame) loginPart3(userAccount Account, page parser.OverviewPage) error
go func(b *OGame) {
defer atomic.StoreInt32(&b.chatConnectedAtom, 0)
chatRetry := NewExponentialBackoff(context.Background(), 60)
LOOP:
for {
chatRetry.LoopForever(func() bool {
select {
case <-b.closeChatCh:
break LOOP
return false
default:
b.connectChat(chatRetry, chatHost, chatPort)
chatRetry.Wait()
}
}
return true
})
}(b)
} else {
b.ReconnectChat()
Expand Down

0 comments on commit 8feef4b

Please sign in to comment.