Complete worked example
python
def retry_delay(status, attempt, retry_after=None):
if type(status) is not int or not 100 <= status <= 599:
raise ValueError("HTTP status required")
if type(attempt) is not int or not 0 <= attempt <= 5:
raise ValueError("attempt must be 0..5")
if status not in {429, 502, 503, 504} or attempt >= 3:
return None
if retry_after is not None:
if not isinstance(retry_after, str) or not retry_after.isascii() or not retry_after.isdecimal():
raise ValueError("this contract accepts delay-seconds only")
delay = int(retry_after)
return max(0.25, delay) if delay <= 60 else None
return min(4.0, 0.25 * 2 ** attempt)- Reasoning
- Calculate a bounded retry decision without sleeping, inventing success or shortening a server-requested delay. Compare the stated values and failure boundary before changing the implementation.