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. Its 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.
  • Exponential backoff with jitter between retries.
  • Limit the number of retries and total time.
  • Automatic async support.
  • Preserve type hints of the decorated callable.
  • Flexible instrumentation with Prometheus, structlog, and standard library's logging support out-of-the-box.
  • Easy global deactivation for testing.

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

  • Instrumentation is now pluggable! You can define your own hooks that are run with retry details whenever a retry is scheduled. The documentation now has a whole chapter on instrumentation. #37

  • If structlog is not installed, the scheduled retry is now logged using the standard library logging module by default. #35

Changed

  • Tenacity's internal AttemptManager object is no longer exposed to the user. This was an oversight and never documented. stamina.retry_context() now yields instances of stamina.Attempt. #22

  • Initialization of instrumentation is now delayed. This means that if there's no retries, there's no startup overhead from importing structlog and prometheus-client. #34

  • Some key names in structlog log messages have been renamed to better reflect their meaning (sleptwaited_so_far, attemptretry_num, and errorcaused_by). You can rename them back using structlog's structlog.processors.EventRenamer. #35


→ 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.

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

Uploaded Source

Built Distribution

stamina-23.2.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stamina-23.2.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for stamina-23.2.0.tar.gz
Algorithm Hash digest
SHA256 69e61a2d303d3fc2a54ac4c4c4b86da916bc301a2a03f4d2e2a5b3bb48e4f5cc
MD5 900a8fe475f2b2255eafd87889e19f12
BLAKE2b-256 d96dffda37694992b0e5aad604c369cb01245bcc5e163823ec6ff97a1ae0496e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stamina-23.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for stamina-23.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31af7998497156af39e08d061a66c94643d037d90f4510ab8d03ece7526afb66
MD5 8da46a3d38b6cd8faa3ab8fbcd171169
BLAKE2b-256 43fa65c058f6e76dc99769ebb256ebc939582c9cc1f7d193511f4352e70f1d7e

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