Skip to main content

rateguard-client (Python)

A client for rateguard-sidecar: one global rate limit across a fleet, from Python. Standard library only, Python 3.9 or newer.

pip install rateguard-client
from rateguard_client import Client

limiter = Client()  # http://127.0.0.1:7947, or $RATEGUARD_SIDECAR_URL

decision = limiter.check("tenant:42", cls="pro")
if not decision.allowed:
    return Response(status=429, headers={"Retry-After": str(math.ceil(decision.retry_after))})
Option Default Meaning
url $RATEGUARD_SIDECAR_URL or http://127.0.0.1:7947 The sidecar
timeout 0.05 Seconds to wait for an answer
on_error "allow" "allow" or "deny" the request when the sidecar cannot answer
admin_timeout 1.0 Seconds to wait for every call but check

A broken limiter should not take the service down with it, hence allow by default. Whenever the sidecar gives no decision, decision.error says why (it is None otherwise, and does not take part in comparing decisions). An empty url or $RATEGUARD_SIDECAR_URL counts as unset.

A key over the sidecar's 64 KiB limit is the one exception: the sidecar refuses it with a 413 every time, so the client denies it (allowed=False, retry_after=0, with an error) whatever on_error says, rather than let it past the limit for good.

One keep-alive connection per thread, closed when the thread ends; client.close(), or with Client() as limiter:, closes every thread's.

Stats and limits

The rest of the sidecar's API, off the request path. Limits and answers are the JSON of the sidecar's README, as dicts; the calls that change limits need the sidecar started with --admin.

stats = limiter.stats()                  # admitted, denied, most_denied by name, ...
one = limiter.key_stats("tenant:42", cls="pro")  # None while the key is not busy here

limiter.set_classes({"free": {"per_second": 10}, "pro": {"per_minute": 60000, "burst": 50}})
limiter.set_limit({"per_second": 100})  # the default class
limiter.set_key_limits([
    {"class": "pro", "key": "tenant:42", "limit": {"per_minute": 90000}},
    {"key": "tenant:7", "as_class": "pro"},
])
limiter.set_shadows({"free": {"per_second": 5}})  # counts what it would deny
limiter.set_dry_run(False)

Each set_ call replaces what was there: every class, every key's exception, every shadow. These calls wait admin_timeout seconds, one by default, and raise SidecarError when the sidecar does not carry them out, with its reason and status, the HTTP status, or None when it did not answer; they never follow on_error.

Tests: PYTHONPATH=src python3 -m unittest discover -s tests.

Metadata

Release files for rateguard-client 0.6.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 rateguard-client 0.6.0
File Size Uploaded Provenance Actions
rateguard_client-0.6.0.tar.gz 9.7 kB Without provenance Details for rateguard_client-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rateguard-client 0.6.0
File Interpreter ABI Platform Actions
rateguard_client-0.6.0-py3-none-any.whl Python 3 none any Details for rateguard_client-0.6.0-py3-none-any.whl

Total release size: 16.2 kB

Release files / rateguard_client-0.6.0.tar.gz

Download URL rateguard_client-0.6.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0960f7122101dcf9361c24c3f2de1c0c519f6527cc3d3bed8a74fc7766abe7ce
BLAKE2b-256 checksum
How to use checksums
78b9396533c250dc5ba99a85878424b0b00dc66606a448769b20ad6f448cb366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release files / rateguard_client-0.6.0-py3-none-any.whl

Download URL rateguard_client-0.6.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b06988fac287f32433e86eeb3dcbb3c885abd629a1d809aa8ffa51be021c637
BLAKE2b-256 checksum
How to use checksums
f5b818595b49c6c0c7d6c5fac557228d87b3194a500fd7f8bc0398d80d71d955
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Security for rateguard-client 0.6.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