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

Allow request retry with custom HTTP codes (like 429 - Too Many Requests) and implement delay retry #3

Closed
tciuro opened this issue Nov 22, 2021 · 1 comment · Fixed by #4
Assignees
Labels
enhancement New feature or request

Comments

@tciuro
Copy link
Contributor

tciuro commented Nov 22, 2021

Q A
BC Break no
Version 0.9.15

Support Question

When a service returns HTTP 429 Too Many Requests, I was expecting RealHTTP to trigger the retry mechanism. Alas, this is not what I see. I get an HTTP 429 error right away without any retries. Shouldn't this scenario be handled by RealHTTP? 🤔

malcommac added a commit that referenced this issue Nov 28, 2021
@malcommac
Copy link
Collaborator

Hi @tciuro,
In fact the current implementation of the HTTPDefaultValidator does not implement a way to retry based on HTTP status code. However is pretty simple to implement it and I think it could be useful.
So I've added it to the main repository and I'll push a new release.

Currently HTTPDefaultValidator exposes a new property:

  • retriableHTTPStatusCodes: it defines a list of HTTPStatusCode which can trigger a retry of the call; it's a dictionaryw where the value is a TimeInterval (0 means retry immediately, a positive value like 2 means 2 seconds after retry).

Default implementation set .tooManyRequests's http status code with 3 seconds interval.
You can however set it as you need by modifying the client's HTTPDefaultValidator or provide your own subclass.

This is an example which works with the default shared HTTPClient but you can use it with your own instance.

let myValidator = HTTPDefaultValidator()
myValidator.retriableHTTPStatusCodes = [.tooManyRequests: 5] // wait 5 seconds for HTTP 429
HTTPClient.shared.validators = [myValidator]
        
request.maxRetries = 2 // you must set a retry > 1 in order to get it works
request.onRawResponse { response in
  // do something
}
request.run()

It will be part of the 0.9.16 version.

@malcommac malcommac changed the title Retry mechanism doesn't trigger with HTTP 429 Too Many Requests Allow request retry with custom HTTP codes (like 429 - Too Many Requests) and implement delay retry Nov 28, 2021
@malcommac malcommac self-assigned this Nov 28, 2021
@malcommac malcommac added the enhancement New feature or request label Nov 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants