Problem Statement
Outline a safe retry pattern for transient errors such as network timeouts.
Explanation
Catch known transient exceptions and retry with exponential backoff. Limit the number of attempts and add jitter to avoid thundering herds. Between attempts, release resources and check cancellation signals.
Log the final failure with context and re-raise a clear error. Do not retry on non-transient errors like authentication failures, and do not swallow exceptions silently.
Code Solution
SolutionRead Only
for attempt in range(5):
try:
return call()
except TimeoutError:
sleep(0.1 * 2**attempt)
raise RuntimeError('give up')