Skip to main content

blockguard

A circuit breaker for rate limits the other server imposes on you — not a client-side throttle for your own call rate.

The problem

Libraries like ratelimit or django-ratelimit cap how fast you call out ("no more than 10 requests/second"). That's a different problem from what this solves: a remote server — often one with an undocumented, time-varying, IP-scoped rate limit (scraping targets, flaky third-party APIs, anything without a published SLA) — has already told you "no" with a 429 or a silent block, and now every retry you make while it's still blocking you risks extending the block instead of clearing it.

This happened for real, repeatedly, building a tool against the Steam Community Market: a single user action could fire dozens of requests at an already-flagged endpoint, turning a minutes-long block into one that lasted hours. blockguard is the fix that came out of that, generalized.

Install

pip install blockguard

Usage

from blockguard import CircuitBreaker

breaker = CircuitBreaker(base_cooldown_s=60.0, max_cooldown_s=1800.0)

if breaker.in_cooldown("some_api"):
    return cached_result  # don't even try -- we already know it'll fail

resp = call_flaky_api()
if resp.status_code == 429:
    breaker.trip("some_api")   # next cooldown doubles: 60s, 120s, 240s, ... capped
else:
    breaker.clear("some_api")  # back to a clean slate after a real success

Or with the context-manager shortcut, which raises instead of letting you forget the check:

from blockguard import CircuitBreaker, InCooldownError

breaker = CircuitBreaker()

try:
    with breaker.guard("some_api"):
        resp = call_flaky_api()
except InCooldownError as e:
    print(f"skipping call, {e.seconds_remaining:.0f}s left")

Keys ("families") are independent: tripping "inventory" never affects "search". Use one key per logical endpoint/rate-limit budget, not one global key for your whole app.

Testing with blockguard in your own test suite

A breaker that lives at module/app scope persists across your whole test session by default — a test that simulates a real block leaves a real cooldown that leaks into the next test. blockguard ships a pytest plugin that fixes this automatically once installed:

from blockguard import CircuitBreaker
from blockguard.pytest_plugin import track

breaker = track(CircuitBreaker())  # now reset between every test, automatically

What this is not (yet)

v0.1 is deliberately small: an in-memory, single-process breaker, no async API, no hosted dashboard, no Redis backend for multi-process sharing. If you need any of that, open an issue — it'll ship if there's real demand, not before.

License

MIT

Metadata

Release files for blockguard 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blockguard 0.1.0
File Size Uploaded Provenance Actions
blockguard-0.1.0.tar.gz 7.3 kB Without provenance Details for blockguard-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for blockguard 0.1.0
File Interpreter ABI Platform Actions
blockguard-0.1.0-py3-none-any.whl Python 3 none any Details for blockguard-0.1.0-py3-none-any.whl

Total release size: 14.3 kB

Release files / blockguard-0.1.0.tar.gz

Download URL blockguard-0.1.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d739d4b6336f6ad0138e77f701566e0f67c05b58d9f1be131a7c9fcb900d1032
BLAKE2b-256 checksum
How to use checksums
c698e8c2fbb1a6ffee13f6883ab76892714123c80410e91dad622839a4e0f52b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release files / blockguard-0.1.0-py3-none-any.whl

Download URL blockguard-0.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98699a790b53b1468620642815178757cf5ee64d3f35b2778af6fe61df8dbc90
BLAKE2b-256 checksum
How to use checksums
51d0bcb8635319120e0332738fc772708d58b18d888a4218fca821384a877b08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Security for blockguard 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page