Skip to content

fix: Do first try immediately, then activate floor #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Retrier struct {
// New creates a retrier that exponentially backs off from floor to ceil pauses.
func New(floor, ceil time.Duration) *Retrier {
return &Retrier{
delay: floor,
delay: 0,
floor: floor,
ceil: ceil,
}
Expand All @@ -29,6 +29,9 @@ func (r *Retrier) Wait(ctx context.Context) bool {
}
select {
case <-time.After(r.delay):
if r.delay < r.floor {
r.delay = r.floor
}
return true
case <-ctx.Done():
return false
Expand Down
14 changes: 14 additions & 0 deletions retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ func TestContextCancel(t *testing.T) {
t.Fatalf("attempt allowed even though context cancelled")
}
}

func TestFirstTryImmediately(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r := New(time.Hour, time.Hour)
tt := time.Now()
if !r.Wait(ctx) {
t.Fatalf("attempt not allowed")
}
if time.Since(tt) > time.Second {
t.Fatalf("attempt took too long")
}
}