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
    except SomeError:
    raise MaxRetriesError

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, None]]


Return the result of yield_attempts().

This allows policies to be directly iterated over.

Return type

Iterator[Optional[float, None]]

static new(attempts=None, budget=None, backoff=None)[source]

Create a new retry policy with the given constraints.

  • attempts (Optional[int, None]) – The maximum number of times the operation can be attempted.

  • budget (Optional[float, None]) – The maximum amount of time, in seconds, that the local service will wait for the operation to succeed.

  • backoff (Optional[float, None]) – The base amount of time, in seconds, for exponential back-off between attempts. N in (N * 2**attempts).

Return type