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 RetryUntil to timed package #57

Merged
merged 1 commit into from
Aug 17, 2022
Merged

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jul 27, 2022

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.

@cla-checker-service
Copy link

cla-checker-service bot commented Jul 27, 2022

💚 CLA has been signed

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jul 27, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-08-08T13:09:49.625+0000

  • Duration: 9 min 20 sec

Test stats 🧪

Test Results
Failed 0
Passed 934
Skipped 0
Total 934

@kvch kvch marked this pull request as ready for review July 27, 2022 09:48
@kvch
Copy link
Contributor Author

kvch commented Jul 27, 2022

I've signed the CLA.

Copy link

@urso urso left a 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()

@kvch
Copy link
Contributor Author

kvch commented Jul 28, 2022

@cmacknz Could you please take a look at this PR? We use this lib, and thought we could contribute some code back. :)

@cmacknz
Copy link
Member

cmacknz commented Jul 28, 2022

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?

@cmacknz
Copy link
Member

cmacknz commented Aug 3, 2022

/test

1 similar comment
@cmacknz
Copy link
Member

cmacknz commented Aug 8, 2022

/test

@cmacknz cmacknz merged commit c3eae1c into elastic:main Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants