baseplate.lib.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.lib.retry.RetryPolicy[source]

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()[source]

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.

Return type:Iterator[Optional[float]]
__iter__()[source]

Return the result of yield_attempts().

This allows policies to be directly iterated over.

Return type:Iterator[Optional[float]]
static new(attempts=None, budget=None, backoff=None)[source]

Create a new retry policy with the given constraints.

Parameters:
  • attempts (Optional[int]) – The maximum number of times the operation can be attempted.
  • budget (Optional[float]) – The maximum amount of time, in seconds, that the local service will wait for the operation to succeed.
  • backoff (Optional[float]) – The base amount of time, in seconds, for exponential back-off between attempts. N in (N * 2**attempts).
Return type:

RetryPolicy