Skip to main content

In-memory URL backoff registry with sliding window thresholds.

Project description

url-backoff-registry

PyPI version CI

In-memory URL backoff registry with sliding window thresholds.

Track failing endpoints and back off when failures exceed a threshold within a time window. Useful for avoiding repeated requests to flaky or overloaded services.

Install

pip install url-backoff-registry

Usage

from url_backoff_registry import BackoffRegistry

# Back off for 120s after 3 failures within 30s
registry = BackoffRegistry(window_seconds=30, threshold=3, backoff_seconds=120)

def fetch(url):
    if registry.should_backoff(url):
        raise Exception(f"Backing off from {url}")

    try:
        response = requests.get(url)
        response.raise_for_status()
        return response
    except Exception:
        registry.record_failure(url)
        raise

# Check when backoff ends
if registry.should_backoff(url):
    retry_at = registry.next_retry_at(url)
    print(f"Retry after {retry_at}")

# Clear backoff manually (e.g., after a successful request)
registry.clear(url)

API

BackoffRegistry

Parameter Type Default Description
window_seconds int 30 Time window for counting failures
threshold int 3 Number of failures to trigger backoff
backoff_seconds int 120 How long to back off
clock callable datetime.utcnow Clock function (for testing)

Methods

  • record_failure(key) - Record a failure for the given key
  • should_backoff(key) - Returns True if currently in backoff
  • next_retry_at(key) - Returns datetime when backoff ends, or None
  • clear(key) - Clear backoff and failure history for the key

FAQ

How is this different from the backoff package?

The backoff package provides decorators for retrying a single function call with exponential backoff. It's great for "retry this request up to 3 times with increasing delays."

This package solves a different problem: tracking failures across multiple calls to decide whether to attempt a request at all. It answers "should I even try this URL right now, given its recent failure history?"

backoff url-backoff-registry
Scope Single function call Cross-call state
Mechanism Retry decorator Failure registry
Question answered "How many times should I retry?" "Should I try at all?"

They're complementary - you can use both together.

Why not just use a circuit breaker?

Circuit breakers (like pybreaker) are similar but typically operate per-function. This registry is keyed by URL/endpoint, so you can track failures for many endpoints with a single registry instance. It's lighter weight and doesn't require decorating each call site.

Development

git clone https://github.com/larsderidder/url-backoff-registry.git
cd url-backoff-registry
python -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
pytest

License

MIT

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

url_backoff_registry-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

url_backoff_registry-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file url_backoff_registry-0.1.0.tar.gz.

File metadata

  • Download URL: url_backoff_registry-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for url_backoff_registry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f2e3d3ee6a6cd0f11cc559a830840caeeca6d7b613ac386a0bfed1befa1e12b
MD5 836c81ca7a2b03aab3307872fb3250a4
BLAKE2b-256 775905499ae9c98e28df5407d12cea9dcce0652e4be2356a7d627573098fc1cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for url_backoff_registry-0.1.0.tar.gz:

Publisher: publish.yml on larsderidder/url-backoff-registry

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

File details

Details for the file url_backoff_registry-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for url_backoff_registry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e5aadb241ee5a7f30c3ae478eb58283a9702734c98a58645509ae0ab27cc146
MD5 3c46442be9fac63565bd49780be2f15e
BLAKE2b-256 2c0bee3d8322f5addbaf8ebe03df5e9d31ac0cd5069724dbbd045b56a5bd3270

See more details on using hashes here.

Provenance

The following attestation bundles were made for url_backoff_registry-0.1.0-py3-none-any.whl:

Publisher: publish.yml on larsderidder/url-backoff-registry

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 Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page