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

Update retry.go to handle hung operations #147

Closed
wants to merge 5 commits into from
Closed
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
68 changes: 46 additions & 22 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (o Operation) withEmptyData() OperationWithData[struct{}] {
// the notify function isn't called.
type Notify func(error, time.Duration)

type OperationResult[T any] struct {
res T
err error
}
// Retry the operation o until it does not return error or BackOff stops.
// o is guaranteed to be run at least once.
//
Expand Down Expand Up @@ -84,35 +88,55 @@ func doRetryNotify[T any](operation OperationWithData[T], b BackOff, notify Noti
ctx := getContext(b)

b.Reset()
for {
res, err = operation()
if err == nil {
return res, nil
}

var permanent *PermanentError
if errors.As(err, &permanent) {
return res, permanent.Err
}

for {
outCh := make(chan OperationResult[T])

// Launch the operation asynchronously
go func() {
res, err := operation()
outCh <- OperationResult[T]{res, err}
}()

// Then wait for a) overall timeout, b) error, c) result
select {
case <-ctx.Done():
return res, ctx.Err()
case out := <-outCh:
res = out.res
err = out.err

if next = b.NextBackOff(); next == Stop {
if cerr := ctx.Err(); cerr != nil {
return res, cerr
if err == nil {
return res, nil
}

var permanent *PermanentError
if errors.As(err, &permanent) {
return res, permanent.Err
}

return res, err
}
if next = b.NextBackOff(); next == Stop {
if cerr := ctx.Err(); cerr != nil {
return res, cerr
}

if notify != nil {
notify(err, next)
}
return res, err
}

t.Start(next)
if notify != nil {
notify(err, next)
}

select {
case <-ctx.Done():
return res, ctx.Err()
case <-t.C():
// Start a timer to delay before the next try
t.Start(next)

// But exit, if our overall context times out while waiting for the next attempt
select {
case <-ctx.Done():
return res, ctx.Err()
case <-t.C():
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ func TestRetry(t *testing.T) {
}
}

func TestRetryHangs(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

f := func() error {
log.Printf("Test Hang")
time.Sleep(10 * time.Second)
return nil
}

err := RetryNotifyWithTimer(f, WithContext(NewConstantBackOff(time.Millisecond), ctx), nil, &testTimer{})
if err == nil {
t.Errorf("error is unexpectedly nil")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("unexpected error: %s", err.Error())
}
}

func TestRetryWithData(t *testing.T) {
const successOn = 3
var i = 0
Expand Down
Loading