Skip to content

Commit

Permalink
cancelable exp backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 21, 2022
1 parent d11813b commit b6b8a3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions pkg/wrapper/exponentialBackoff.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package wrapper

import "time"
import (
"context"
"time"
)

// ExponentialBackoff ...
type ExponentialBackoff struct {
ctx context.Context
val int
max int
}

// NewExponentialBackoff ...
func NewExponentialBackoff(max int) *ExponentialBackoff {
func NewExponentialBackoff(ctx context.Context, max int) *ExponentialBackoff {
if max < 0 {
max = 0
}
e := new(ExponentialBackoff)
e.ctx = ctx
e.max = max
return e
}
Expand All @@ -23,7 +28,11 @@ func (e *ExponentialBackoff) Wait() {
if e.val == 0 {
e.val = 1
} else {
time.Sleep(time.Duration(e.val) * time.Second)
select {
case <-time.After(time.Duration(e.val) * time.Second):
case <-e.ctx.Done():
return
}
e.val *= 2
if e.max > 0 {
if e.val > e.max {
Expand Down
2 changes: 1 addition & 1 deletion pkg/wrapper/ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ func (b *OGame) loginPart3(userAccount Account, page parser.OverviewPage) error
b.closeChatCh = make(chan struct{})
go func(b *OGame) {
defer atomic.StoreInt32(&b.chatConnectedAtom, 0)
chatRetry := NewExponentialBackoff(60)
chatRetry := NewExponentialBackoff(context.Background(), 60)
LOOP:
for {
select {
Expand Down

0 comments on commit b6b8a3e

Please sign in to comment.