Skip to content
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

Add a SurfaceWorkErrors() opt to the retrier #51

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 17 additions & 6 deletions retrier/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
// Retrier implements the "retriable" resiliency pattern, abstracting out the process of retrying a failed action
// a certain number of times with an optional back-off between each retry.
type Retrier struct {
backoff []time.Duration
infiniteRetry bool
class Classifier
jitter float64
rand *rand.Rand
randMu sync.Mutex
backoff []time.Duration
infiniteRetry bool
surfaceWorkErrors bool
class Classifier
jitter float64
rand *rand.Rand
randMu sync.Mutex
}

// New constructs a Retrier with the given backoff pattern and classifier. The length of the backoff pattern
Expand All @@ -43,6 +44,13 @@ func (r *Retrier) WithInfiniteRetry() *Retrier {
return r
}

// WithSurfaceWorkErrors configures the retrier to always return the last non-nil error received from work()
eapache marked this conversation as resolved.
Show resolved Hide resolved
// even if a context timeout/deadline is hit.
func (r *Retrier) WithSurfaceWorkErrors() *Retrier {
r.surfaceWorkErrors = true
return r
}

// Run executes the given work function by executing RunCtx without context.Context.
func (r *Retrier) Run(work func() error) error {
return r.RunFn(context.Background(), func(c context.Context, r int) error {
Expand Down Expand Up @@ -83,6 +91,9 @@ func (r *Retrier) RunFn(ctx context.Context, work func(ctx context.Context, retr

timer := time.NewTimer(r.calcSleep(retries))
if err := r.sleep(ctx, timer); err != nil {
if r.surfaceWorkErrors && ret != nil {
eapache marked this conversation as resolved.
Show resolved Hide resolved
return ret
}
return err
}

Expand Down
38 changes: 38 additions & 0 deletions retrier/retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,44 @@ func TestRetrierRunFnWithInfinite(t *testing.T) {
}
}

func TestRetrierCtxSurfaceWorkErrors(t *testing.T) {
eapache marked this conversation as resolved.
Show resolved Hide resolved
// Will timeout before getting to errBar.
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
defer cancel()
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
errExpected := []error{errFoo, errFoo, errBar, errBaz}
retries := 0
err := r.RunCtx(ctx, func(ctx context.Context) error {
if retries >= len(errExpected) {
return nil
}
err := errExpected[retries]
retries++
return err
})
if err != errFoo {
t.Error(err)
}
}

func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) {
// Will timeout before getting to errBar.
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
eapache marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
errExpected := []error{errFoo, errFoo, errFoo, errBar, errBaz}

err := r.RunFn(ctx, func(ctx context.Context, retries int) error {
if retries >= len(errExpected) {
return nil
}
return errExpected[retries]
})
if err != errFoo {
t.Error(err)
}
}

func TestRetrierNone(t *testing.T) {
r := New(nil, nil)

Expand Down
Loading