Skip to content

Commit

Permalink
Merge branch 'release/48.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 22, 2022
2 parents 4807b98 + 18bed65 commit 261fa53
Show file tree
Hide file tree
Showing 32 changed files with 4,003 additions and 3,534 deletions.
14 changes: 14 additions & 0 deletions doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pkg/ogame
Define all ogame data structures

pkg/client
Http client that communicate with ogame server to retrieve html pages

pkg/extractor
Extract information out of a html page

pkg/parser
Given an extractor and html page, return a typed page with methods to extract data on that page

pkg/wrapper
Build logic and utilities using pkg/ogame data structures
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/PuerkitoBio/goquery v1.5.1
github.com/alaingilbert/clockwork v0.1.1-0.20200117075841-891256a24209
github.com/alaingilbert/clockwork v0.1.1-0.20220821091847-24a25c94a5fe
github.com/dustin/go-humanize v1.0.0
github.com/go-errors/errors v1.0.1
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/abiosoft/ishell v2.0.0+incompatible h1:zpwIuEHc37EzrsIYah3cpevrIc8Oma
github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530=
github.com/alaingilbert/clockwork v0.1.1-0.20200117075841-891256a24209 h1:X2PLaXYK9kPDScClx4IdBOZvJsRHx6LnA++VCa0lY58=
github.com/alaingilbert/clockwork v0.1.1-0.20200117075841-891256a24209/go.mod h1:YeWgj4cWbHxtx84adUefC05mTiKGZfjKNwGGVnA1W6g=
github.com/alaingilbert/clockwork v0.1.1-0.20220821091847-24a25c94a5fe h1:/Ta9Gu3ocbtO0X646f7XmgDftVl5qzRZFlM9StHAyr8=
github.com/alaingilbert/clockwork v0.1.1-0.20220821091847-24a25c94a5fe/go.mod h1:0vLst+y08EBnefROgmTXZLo85oM7nvLd20gnzeR34GQ=
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
Expand Down
71 changes: 71 additions & 0 deletions pkg/exponentialBackoff/exponentialBackoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package exponentialBackoff

import (
"context"
"github.com/alaingilbert/clockwork"
"sync/atomic"
"time"
)

// ExponentialBackoff ...
type ExponentialBackoff struct {
ctx context.Context
clock clockwork.Clock
val uint32 // atomic
max int
}

// New ...
func New(ctx context.Context, clock clockwork.Clock, max int) *ExponentialBackoff {
if max < 0 {
max = 0
}
e := new(ExponentialBackoff)
e.ctx = ctx
e.clock = clock
e.max = max
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() {
currVal := atomic.LoadUint32(&e.val)
if currVal == 0 {
atomic.StoreUint32(&e.val, 1)
return
}

newVal := currVal * 2
if e.max > 0 && newVal > uint32(e.max) {
newVal = uint32(e.max)
}
atomic.StoreUint32(&e.val, newVal)
select {
case <-e.clock.After(time.Duration(currVal) * time.Second):
case <-e.ctx.Done():
return
}
}

// Reset ...
func (e *ExponentialBackoff) Reset() {
atomic.StoreUint32(&e.val, 0)
}
38 changes: 38 additions & 0 deletions pkg/exponentialBackoff/exponentialBackoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package exponentialBackoff

import (
"context"
"github.com/alaingilbert/clockwork"
"github.com/magiconair/properties/assert"
"sync"
"sync/atomic"
"testing"
"time"
)

func TestExponentialBackoff_Wait(t *testing.T) {
var counter uint32
clock := clockwork.NewFakeClock()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
clock.BlockUntil(1)
clock.Advance(1000 * time.Millisecond)
clock.BlockUntil(0)
clock.BlockUntil(1)
clock.Advance(2000 * time.Millisecond)
clock.BlockUntil(0)
clock.BlockUntil(1)
clock.Advance(4000 * time.Millisecond)
clock.BlockUntil(0)
atomic.AddUint32(&counter, 1)
wg.Done()
}()
e := New(context.Background(), clock, 60)
e.Wait() // First time has no wait
e.Wait() // Wait 1s
e.Wait() // Wait 2s
e.Wait() // Wait 4s
wg.Wait()
assert.Equal(t, uint32(1), atomic.LoadUint32(&counter))
}
Loading

0 comments on commit 261fa53

Please sign in to comment.