Skip to content

context timeout via <-ctx.Done() #95

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

Closed
georgysavva opened this issue Mar 29, 2020 · 4 comments
Closed

context timeout via <-ctx.Done() #95

georgysavva opened this issue Mar 29, 2020 · 4 comments

Comments

@georgysavva
Copy link

As I can understand from the code, Retry() function can be canceled via context timeout: here
And this check is inside the retry loop. But in case the actual operation is a long running operation or it can even block, the retry function won't be canceled after context timeout, because it will hang on operation for a long time.
I think, that In order to achieve a strict cancelation we should listen for ctx.Done() in a separate gorutine outside of the retry loop.
Is it intentionally or not?

@georgysavva
Copy link
Author

Also what one should use to restrict total time for retries? context timeout or MaxElapsedTime. As I can see they serve pretty similar purpose.
But for context you check the next interval, that it won't exceed the deadline: here
And you don't do the same for elapsedTime: here

@cenkalti
Copy link
Owner

cenkalti commented Apr 3, 2020

This is an old package and Retry function is created at a time when context package did not exists. That's why Operation function signature does not accept a context.Context parameter. If it blocks, it blocks and retrying can only be cancelled after it returns. For a strict cancellation, I recommend creating a separate context.Context with a timeout/deadline and pass to your blocking function and use backoff.WithContext function to wrap your backoff.BackOff instance. Please let me know if you need an example, I can write one.

I am going to update ExponentionBackoff.NextBackOff method to reflect the change you have asked. Can you please take a look. If it's okay I will tag the commit as new version.

@georgysavva
Copy link
Author

Everything is clear, thanks.
I saw the commit, it's exactly what I was asking for. Thank!

@cenkalti
Copy link
Owner

cenkalti commented Apr 3, 2020

Tagged the commit as v4.0.2.

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

No branches or pull requests

2 participants