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!

Or, if you prefer video, here's a brief introduction into retries and stamina: Watch the video

Release Information

Added

  • cap argument to stamina.set_testing(). By default, the value passed as attempts is used strictly. When cap=True, it is used as an upper cap; that means that if the original attempts number is lower, it's not changed. #80

  • stamina.set_testing() can now be used as a context manager. #94

  • Instrumentation hooks can now can return context managers. If they do, they are entered when a retry is scheduled and exited right before the retry is attempted. #95


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-25.1.0.tar.gz (561.1 kB view details)

Uploaded Source

Built Distribution

stamina-25.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stamina-25.1.0.tar.gz
  • Upload date:
  • Size: 561.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for stamina-25.1.0.tar.gz
Algorithm Hash digest
SHA256 ad674809796ae40512b3b6296cfade826efd63863ff2ca2f59f806342e91e94a
MD5 be1b05bbf4deb3f92c3d574a5daff9b2
BLAKE2b-256 fdc4d242d76ffc88aa1fd14214d3143b542857b32276db4a20f8d99669054a5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for stamina-25.1.0.tar.gz:

Publisher: pypi-package.yml on hynek/stamina

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: stamina-25.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for stamina-25.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c08291da540e6f4243c20f7ee98f0ed0ac9101d639803c481a029b56d7e9b45d
MD5 59d742056f970f35ed3affe5f60abd44
BLAKE2b-256 14bad03f7ee711391af1d5f4dd7c44f8abdd06bce247028af2441ba8f6ff329b

See more details on using hashes here.

Provenance

The following attestation bundles were made for stamina-25.1.0-py3-none-any.whl:

Publisher: pypi-package.yml on hynek/stamina

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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