-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from toga4/feature/limit-attempts
Change the backoff interfaces to allow limiting number of attempts
- Loading branch information
Showing
14 changed files
with
170 additions
and
121 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
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
35 changes: 16 additions & 19 deletions
35
adapter/github.com/googleapis/gax-go.v2/gaxbackoff/backoff_test.go
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 |
---|---|---|
@@ -1,39 +1,36 @@ | ||
package gaxbackoff | ||
|
||
import ( | ||
"math" | ||
"context" | ||
"log" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBackoff(t *testing.T) { | ||
initialBackoff := 250 * time.Millisecond | ||
maxBackoff := 5 * time.Second | ||
ctx := context.Background() | ||
|
||
initialBackoff := 20 * time.Millisecond | ||
maxBackoff := 50 * time.Millisecond | ||
backoffMultiplier := 2.0 | ||
|
||
backoffConfig := &BackoffPolicy{ | ||
backoffPolicy := &BackoffPolicy{ | ||
Initial: initialBackoff, | ||
Max: maxBackoff, | ||
Multiplier: backoffMultiplier, | ||
} | ||
|
||
b := backoffConfig.New() | ||
|
||
var val time.Duration | ||
|
||
for i := 0; i < 5; i++ { | ||
max := initialBackoff * time.Duration(math.Pow(2, float64(i))) | ||
ctx, cancel := context.WithTimeout(ctx, 110*time.Millisecond) | ||
defer cancel() | ||
|
||
val = b.Pause() | ||
if val > max { | ||
t.Errorf("expected %v to be less than %v", val, max) | ||
} | ||
b := backoffPolicy.New(ctx) | ||
count := 0 | ||
for b.Continue() { | ||
log.Println(count) | ||
count++ | ||
} | ||
|
||
for i := 0; i < 100_000; i++ { | ||
val = b.Pause() | ||
if val > maxBackoff { | ||
t.Errorf("expected %v to be less than %v", val, maxBackoff) | ||
} | ||
if count < 4 { | ||
t.Errorf("expected count is greater than or equal to 4 but %v", count) | ||
} | ||
} |
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
8 changes: 3 additions & 5 deletions
8
adapter/github.com/lestrrat-go/backoff.v2/lestrratbackoff/backoff.go
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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
package lestrratbackoff | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/lestrrat-go/backoff/v2" | ||
) | ||
|
||
type adapter struct { | ||
backoff backoff.IntervalGenerator | ||
controller backoff.Controller | ||
} | ||
|
||
func (a *adapter) Pause() time.Duration { | ||
return a.backoff.Next() | ||
func (a *adapter) Continue() bool { | ||
return backoff.Continue(a.controller) | ||
} |
12 changes: 7 additions & 5 deletions
12
adapter/github.com/lestrrat-go/backoff.v2/lestrratbackoff/constant.go
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package lestrratbackoff | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/lestrrat-go/backoff/v2" | ||
"github.com/toga4/go-retryabletransport" | ||
) | ||
|
||
type constantPolicy struct { | ||
options []backoff.ConstantOption | ||
policy backoff.Policy | ||
} | ||
|
||
func NewConstantPolicy(options ...backoff.ConstantOption) retryabletransport.BackoffPolicy { | ||
return &constantPolicy{options} | ||
func NewConstantPolicy(options ...backoff.Option) retryabletransport.BackoffPolicy { | ||
return &constantPolicy{backoff.Constant(options...)} | ||
} | ||
|
||
func (p *constantPolicy) New() retryabletransport.Backoff { | ||
return &adapter{backoff.NewConstantInterval(p.options...)} | ||
func (p *constantPolicy) New(ctx context.Context) retryabletransport.Backoff { | ||
return &adapter{p.policy.Start(ctx)} | ||
} |
34 changes: 21 additions & 13 deletions
34
adapter/github.com/lestrrat-go/backoff.v2/lestrratbackoff/constant_test.go
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 |
---|---|---|
@@ -1,30 +1,38 @@ | ||
package lestrratbackoff | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lestrrat-go/backoff/v2" | ||
) | ||
|
||
func Test_ConstantPolicy(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
backoffPolicy := NewConstantPolicy( | ||
backoff.WithInterval(1000*time.Millisecond), | ||
backoff.WithJitterFactor(0.1), | ||
backoff.WithInterval(20*time.Millisecond), | ||
backoff.WithMaxRetries(3), | ||
) | ||
|
||
min := 900 * time.Millisecond | ||
max := 1100 * time.Millisecond | ||
b := backoffPolicy.New(ctx) | ||
|
||
start := time.Now() | ||
|
||
b := backoffPolicy.New() | ||
count := 0 | ||
for b.Continue() { | ||
count++ | ||
} | ||
|
||
for i := 0; i < 100_000; i++ { | ||
val := b.Pause() | ||
if val < min { | ||
t.Errorf("expected %v to be greater than %v", val, min) | ||
} | ||
if val > max { | ||
t.Errorf("expected %v to be less than %v", val, max) | ||
} | ||
durationMillis := time.Now().Sub(start) | ||
|
||
if count != 4 { | ||
t.Errorf("expected count is equal to 4 but %v", count) | ||
} | ||
min := (60 - 5) * time.Millisecond | ||
max := (60 + 5) * time.Millisecond | ||
if durationMillis < min || durationMillis > max { | ||
t.Errorf("expected duration is between %v and %v but %v", min, max, durationMillis) | ||
} | ||
} |
10 changes: 6 additions & 4 deletions
10
adapter/github.com/lestrrat-go/backoff.v2/lestrratbackoff/exponential.go
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package lestrratbackoff | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/lestrrat-go/backoff/v2" | ||
"github.com/toga4/go-retryabletransport" | ||
) | ||
|
||
type exponentialPolicy struct { | ||
options []backoff.ExponentialOption | ||
policy backoff.Policy | ||
} | ||
|
||
func NewExponentialPolicy(options ...backoff.ExponentialOption) retryabletransport.BackoffPolicy { | ||
return &exponentialPolicy{options} | ||
return &exponentialPolicy{backoff.Exponential(options...)} | ||
} | ||
|
||
func (p *exponentialPolicy) New() retryabletransport.Backoff { | ||
return &adapter{backoff.NewExponentialInterval(p.options...)} | ||
func (p *exponentialPolicy) New(ctx context.Context) retryabletransport.Backoff { | ||
return &adapter{p.policy.Start(ctx)} | ||
} |
58 changes: 19 additions & 39 deletions
58
adapter/github.com/lestrrat-go/backoff.v2/lestrratbackoff/exponential_test.go
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 |
---|---|---|
@@ -1,60 +1,40 @@ | ||
package lestrratbackoff | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lestrrat-go/backoff/v2" | ||
) | ||
|
||
func Test_ExponentialPolicy(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
backoffPolicy := NewExponentialPolicy( | ||
backoff.WithMinInterval(100*time.Millisecond), | ||
backoff.WithMaxInterval(1*time.Second), | ||
backoff.WithJitterFactor(0.1), | ||
backoff.WithMinInterval(20*time.Millisecond), | ||
backoff.WithMaxInterval(50*time.Millisecond), | ||
backoff.WithMultiplier(2.0), | ||
backoff.WithMaxRetries(3), | ||
) | ||
|
||
b := backoffPolicy.New() | ||
|
||
val := b.Pause() | ||
b := backoffPolicy.New(ctx) | ||
|
||
{ | ||
min := 90 * time.Millisecond | ||
max := 110 * time.Millisecond | ||
start := time.Now() | ||
|
||
if val < min { | ||
t.Errorf("expected %v to be greater than %v", val, min) | ||
} | ||
if val > max { | ||
t.Errorf("expected %v to be less than %v", val, max) | ||
} | ||
count := 0 | ||
for b.Continue() { | ||
count++ | ||
} | ||
|
||
for i := 0; i < 3; i++ { | ||
interval := val * 2.0 | ||
min := time.Duration(float64(interval) * 0.9) | ||
max := time.Duration(float64(interval) * 1.1) | ||
|
||
val = b.Pause() | ||
if val < min { | ||
t.Errorf("expected %v to be greater than %v", val, min) | ||
} | ||
if val > max { | ||
t.Errorf("expected %v to be less than %v", val, max) | ||
} | ||
} | ||
durationMillis := time.Now().Sub(start) | ||
|
||
min := 900 * time.Millisecond | ||
max := 1100 * time.Millisecond | ||
|
||
for i := 0; i < 100_000; i++ { | ||
val := b.Pause() | ||
if val < min { | ||
t.Errorf("expected %v to be greater than %v", val, min) | ||
} | ||
if val > max { | ||
t.Errorf("expected %v to be less than %v", val, max) | ||
} | ||
if count != 4 { | ||
t.Errorf("expected count is equal to 4 but %v", count) | ||
} | ||
min := (110 - 5) * time.Millisecond | ||
max := (110 + 5) * time.Millisecond | ||
if durationMillis < min || durationMillis > max { | ||
t.Errorf("expected duration is between %v and %v but %v", min, max, durationMillis) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package retryabletransport | ||
|
||
import ( | ||
"time" | ||
"context" | ||
) | ||
|
||
// Backoff is an interface that generates next backoff interval. | ||
type Backoff interface { | ||
// Pause returns next backoff interval. | ||
Pause() time.Duration | ||
// Continue returns when to run the next backoff. The 1st call should return | ||
// true immediately. The next and subsequent calls should return whether or | ||
// not the next should be run after a backoff timeout. | ||
Continue() bool | ||
} | ||
|
||
// BackoffPolicy is an interface that generates new Backoff instance. | ||
type BackoffPolicy interface { | ||
// New returns new Backoff instance. | ||
New() Backoff | ||
New(ctx context.Context) Backoff | ||
} |
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
Oops, something went wrong.