-
Notifications
You must be signed in to change notification settings - Fork 14
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 RetryUntil
to timed package
#57
Conversation
💚 CLA has been signed |
I've signed the CLA. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
This change also enables a pattern like this:
tg := TaskGroupWithCancel(ctx)
// this stop handler would also be nice to have in go-concert
tg.OnStop = func(err error) (unison.TaskGroupStopAction, error) {
if err == nil || err == context.Cancelled { // stop all workers on success or if the context got cancelled
return unison.TaskGroupStopActionShutdown, nil
}
// continue other tasks if we've seen one client fail or timeout
return unison.TaskGroupStopActionContinue, err
}
for _, client := range clients {
tg.Go(func(ctx context.Context) error {
return timed.RetryUntil(ctx, 1 * time.Minute, 200 * time.Millisecond, func(ctx context.Context) error {
return client.Send(ctx, ...)
})
})
// wait max 250ms for the send operation to succeed, otherwise we attempt a concurrent a send on the next client
if err := timed.Wait(tg.Context(), 250 * time.Millisecond); err != nil {
// the taskgroup was cancelled because one client did succeed or the parent context got cancelled , stop immediately
break
}
}
return tg.Stop()
@cmacknz Could you please take a look at this PR? We use this lib, and thought we could contribute some code back. :) |
It seems I don't have permissions to do anything on this repository. @v1v are you able to add me as an admin for this repository? |
/test |
1 similar comment
/test |
The new function
RetryUntil
executes a function periodically until it succeeds or the timeout is elapsed. It can be used when you have to wait until some condition becomes true, or something no longer returns an error.