-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
4,003 additions
and
3,534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.