baseplate.retry
¶
Note
This module is a low-level helper, many client libraries have protocol-aware retry logic built in. Check your library before using this.
Policies for retrying an operation safely.
-
class
baseplate.retry.
RetryPolicy
¶ A policy for retrying operations.
Policies are meant to be used as an iterable:
for time_remaining in RetryPolicy.new(attempts=3): try: some_operation.do(timeout=time_remaining) break except SomeError: pass else: raise MaxRetriesError
-
yield_attempts
()¶ Return an iterator which controls attempts.
On each iteration, the iterator will yield the number of seconds left to retry, this should be used to set the timeout on the operation being carried out. If there is no maximum time remaining,
None
is yielded instead.The iterable will raise
StopIteration
once the operation should not be retried any further.
-
__iter__
()¶ Return the result of
yield_attempts()
.This allows policies to be directly iterated over.
-
static
new
(attempts=None, budget=None, backoff=None)¶ Create a new retry policy with the given constraints.
Parameters: - attempts (int) – The maximum number of times the operation can be attempted.
- budget (float) – The maximum amount of time, in seconds, that the local service will wait for the operation to succeed.
- backoff (float) – The base amount of time, in seconds, for
exponential backoff between attempts.
N
in (N * 2**attempts
).
-