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

feat: use error information to calculate backoff #15

Closed
wants to merge 8 commits into from

Conversation

aisbergg
Copy link

I like the implementation concept of go-retry, but it doesn't support backoff calculation based on the response. My use case: Calculate the backoff delay based on different HTTP responses. For example a 427 HTTP response would take the "Retry-After" header into consideration while a 5XX response would do something different.

Essentially I changed the Backoff interface as such:

type Backoff interface {
	// Next takes the error and returns the time duration to wait and the
	// processed error. A duration less than zero signals the backoff to stop
	// and to not retry again.
	Next(err error) (time.Duration, error)
}

This way the backoff calculation can work on error information (e.g. HTTP response information) and modify the (retry-)error if necessary.

There are some other changes mixed in this PR. I just thought I would show you, maybe you are interested into integrating stuff like this (I know it is quite a lot os changes...).

…f delay

BREAKING CHANGE:
- The interface of Backoff changed and is not compatible with previous versions
  anymore.
- The default backoff mechanism doesn't handle selective `retryable` errors
  anymore. If the backoff mechanism shall only work on a subset of errors then
  the new middleware `WithRetryable` must be used.
BREAKING CHANGE: WithJitter and WithJitterPercent now take an additional
`addOnly` and panic if the specified jitter is invalid.
@sethvargo
Copy link
Owner

Hi @aisbergg

Thank you for opening a PR. Unfortunately we should not change the interface, as there are downstream consumers and packages that depend on the existing interface.

For HTTP Retry-After, I don't think you would need to use this library. You could just sleep for the header's returned value.

I'd definitely accept some of the documentation updates though!

@aisbergg
Copy link
Author

Cherry Pick what you like, I don't mind. The PR is more like 'FYI' style.

Regarding the Retry-After header, the current backoff calculation cannot distinguish between different errors. I could just wait like 60s and hope that this would take care of it, but that would be inappropriate for other errors, like temporary Network errors. I do use my changes for different requests as well, like GraphQL, which may contain retry information inside the payload, so yeah, I need some mechanism to deal with this.

@aisbergg aisbergg closed this Jun 28, 2022
@github-actions
Copy link

This pull request has been automatically locked since there has not
been any recent activity after it was closed. Please open a new
issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants