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

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.1
File Size Uploaded Provenance Actions
blockguard-0.1.1.tar.gz 7.4 kB Without provenance Details for blockguard-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 14.6 kB

Release files / blockguard-0.1.1.tar.gz

Download URL blockguard-0.1.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5f47f7399c945078e97ef1eca2633e72cadffdbd632b4c296c036034cfb3fdc4
BLAKE2b-256 checksum
How to use checksums
79af892f7b4cb66407b6785d78465bbe7a00e8f0de0dcc8f7a31ae80054de996
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.1-py3-none-any.whl

Download URL blockguard-0.1.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
593d7a7f0d9f12b090ac385a4adb62b9bee5c1627222eeb618f10e8854cd2a5b
BLAKE2b-256 checksum
How to use checksums
bcd4d23463d4b4c56758464118a33687ad0449be425bb11ac2514959283cb547
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.1

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