Skip to main content

Production-grade retries made easy.

Project description

stamina: Production-grade Retries Made Easy

Transient failures are common in distributed systems. To make your systems resilient, you need to retry failed operations. But bad retries can make things much worse.

stamina is an opinionated wrapper around the great-but-unopinionated Tenacity package. Our goal is to be as ergonomic as possible, while doing the right thing by default, and minimizing the potential for misuse. It is the result of years of copy-pasting the same configuration over and over again:

  • Retry only on certain exceptions – or even a subset of them by introspecting them first using a predicate.
  • Exponential backoff with jitter between retries.
  • Limit the number of retries and total time.
  • Automatic async support – including Trio.
  • Preserve type hints of the decorated callable.
  • Flexible instrumentation with Prometheus, structlog, and standard library's logging support out-of-the-box.
  • Dedicated support for testing that allows to globally deactivate retries, or to limit the number of retries and to remove backoffs.

For example:

import httpx

import stamina


@stamina.retry(on=httpx.HTTPError, attempts=3)
def do_it(code: int) -> httpx.Response:
    resp = httpx.get(f"https://httpbin.org/status/{code}")
    resp.raise_for_status()

    return resp

Async callables work use the same API and it's possible to retry arbitrary blocks, too. Check out our tutorial for more examples!

Release Information

Added

  • The on argument in all retry functions now can be a callable that takes an exception and returns a bool which decides whether or not a retry should be scheduled. #70

  • stamina.Attempt now has a next_wait attribute that contains the time the next backoff will wait, if the current attempt fails (sans jitter). #72

  • It is now possible to switch stamina into a testing mode using stamina.set_testing(). It disables backoffs and caps the number of retries. #73


Full Changelog →

Credits

stamina is written by Hynek Schlawack and distributed under the terms of the MIT license.

The development is kindly supported by my employer Variomedia AG and all my amazing GitHub Sponsors.

This project would not be possible without the years of incredible work that went into Tenacity.

stamina for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of stamina and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open-source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stamina-24.3.0.tar.gz (558.5 kB view details)

Uploaded Source

Built Distribution

stamina-24.3.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file stamina-24.3.0.tar.gz.

File metadata

  • Download URL: stamina-24.3.0.tar.gz
  • Upload date:
  • Size: 558.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for stamina-24.3.0.tar.gz
Algorithm Hash digest
SHA256 1d763c98962ca11f1729c357422926a750a138e803e7beb9f9d6c99d33d9997d
MD5 cfa8ea681c10b9ebd7831804e7684441
BLAKE2b-256 c835afe1f3467e840f414cfea90993c5a94490a6360eb3653236c3a7de099cf6

See more details on using hashes here.

File details

Details for the file stamina-24.3.0-py3-none-any.whl.

File metadata

  • Download URL: stamina-24.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for stamina-24.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28caf1a5db514256d86e32b60621630552fa9a60dace4e6fb5c78ba15f26236e
MD5 b4674caf08fdb8e876a804c58e1db8c0
BLAKE2b-256 7d81818b0b93812cc0a69a0b17c591dca1a3779aeab7f0ff094dee51f4a6bcd3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page