Should this call be retried
The gateway client decides whether a failed call is worth trying again, or whether retrying would only make things worse.
- Retry a 429 and anything in the 500s — those are the bank being busy or broken.
- Never retry anything else; a 400 will fail the same way forever.
- Three attempts is the ceiling, so an attempt number of 3 or more stops.
- The attempt number counts from 1; anything lower is nonsense and stops too.
should_retry(status: int, attempt: int) → bool
Where you start
def should_retry(status: int, attempt: int) -> bool:
Worked examples
| Call | Result |
|---|---|
should_retry(500, 1) | True |
should_retry(429, 2) | True |
should_retry(503, 3) | False |
should_retry(404, 1) | False |
Hint
Deal with the attempt ceiling first, then the status.
Reference solution in Python
def should_retry(status: int, attempt: int) -> bool:
if attempt < 1 or attempt >= 3:
return False
return status == 429 or 500 <= status <= 599