A lightweight package that provides rate-limited httpx transports.
Project description
HTTPX Limiter
| Package | |
| Meta | |
| Automation |
A lightweight package that provides rate-limited httpx transports.
Installation
The package is published on PyPI. Install it, for example, with
pip install httpx-limiter
Tutorial
You can limit the number of requests made by an HTTPX client using the transports provided in this package. That is useful in situations when you need to make a large number of asynchronous requests against endpoints that implement a rate limit,
Single Rate Limit
The simplest use case is to apply a single rate limit to all requests. If you want to be able to make twenty requests per second, for example, use the following code:
import httpx
from httpx_limiter import AsyncRateLimitedTransport, Rate
async def main():
async with httpx.AsyncClient(
transport=AsyncRateLimitedTransport.create(rate=Rate.create(magnitude=20)),
) as client:
response = await client.get("https://httpbin.org")
[!IMPORTANT] Due to limitations in the design of the underlying leaky bucket implementation, which is used to implement the rate limiting, the magnitude of the rate is also the maximum capacity of the bucket. That means, if you set a rate that is larger than one, a burst of requests equal to that capacity will be allowed. If you do not want to allow any bursts, set the magnitude to one, but the duration to the inverse of your desired rate. If you want to allow twenty requests per second, for example, set the magnitude to 1 and the duration to 0.05 seconds.
Multiple Rate Limits
For more advanced use cases, you can apply different rate limits based on a concrete
implementation of the AbstractRateLimiterRepository. There are two relevant methods
that both get passed the current request. One method needs to identify which rate limit
to apply, and the other method sets the rate limit itself. See the following example:
import httpx
from httpx_limiter import AbstractRateLimiterRepository, AsyncMultiRateLimitedTransport, Rate
class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
"""Apply different rate limits based on the domain being requested."""
def get_identifier(self, request: httpx.Request) -> str:
"""Return the domain as the identifier for rate limiting."""
return request.url.host
def get_rate(self, request: httpx.Request) -> Rate:
"""Apply the same, but independent rate limit to each domain."""
return Rate.create(magnitude=25)
client = httpx.AsyncClient(
transport=AsyncMultiRateLimitedTransport.create(
repository=DomainBasedRateLimiterRepository(),
),
)
[!NOTE] You are free to ignore the request argument and use global information like the time of day to determine the rate limit.
from datetime import datetime, timezone
import httpx
from httpx_limiter import AbstractRateLimiterRepository, AsyncMultiRateLimitedTransport, Rate
class DayNightRateLimiterRepository(AbstractRateLimiterRepository):
"""Apply different rate limits based on the time of day."""
def get_identifier(self, _: httpx.Request) -> str:
"""Identify whether it is currently day or night."""
if 6 <= datetime.now(tz=timezone.utc).hour < 18:
return "day"
return "night"
def get_rate(self, _: httpx.Request) -> Rate:
"""Apply different rate limits during the day or night."""
if self.get_identifier(_) == "day":
return Rate.create(magnitude=10)
return Rate.create(magnitude=100)
Copyright
- Copyright © 2024,2025 Moritz E. Beber.
- Free software distributed under the Apache Software License 2.0.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httpx_limiter-0.3.0.tar.gz.
File metadata
- Download URL: httpx_limiter-0.3.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d0c422edc40d41f882e94718466cbe91d3877097afe67bd3f55a9c0df3ea321
|
|
| MD5 |
fbfa5fc550f290b210bc7f59fe13caa4
|
|
| BLAKE2b-256 |
6f72b8ef470dca30babce55fd9e59756b682999c757417adaf0ee99d846e5705
|
Provenance
The following attestation bundles were made for httpx_limiter-0.3.0.tar.gz:
Publisher:
release.yml on Midnighter/httpx-limiter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_limiter-0.3.0.tar.gz -
Subject digest:
4d0c422edc40d41f882e94718466cbe91d3877097afe67bd3f55a9c0df3ea321 - Sigstore transparency entry: 210098639
- Sigstore integration time:
-
Permalink:
Midnighter/httpx-limiter@716e64f4f84e585bcddf1cb3f19929360df809e9 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/Midnighter
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@716e64f4f84e585bcddf1cb3f19929360df809e9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file httpx_limiter-0.3.0-py3-none-any.whl.
File metadata
- Download URL: httpx_limiter-0.3.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69f6e350456d2fe6eea5a36508098a925df16ef15e3d96d4abddd73fa0017625
|
|
| MD5 |
9fd04fa24db1aeeb94563808a5c88cc3
|
|
| BLAKE2b-256 |
0ff6a71ea5bef3aa9bb34ef6e3b017b40616ceccb60621b234112be39d6fbc79
|
Provenance
The following attestation bundles were made for httpx_limiter-0.3.0-py3-none-any.whl:
Publisher:
release.yml on Midnighter/httpx-limiter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_limiter-0.3.0-py3-none-any.whl -
Subject digest:
69f6e350456d2fe6eea5a36508098a925df16ef15e3d96d4abddd73fa0017625 - Sigstore transparency entry: 210098642
- Sigstore integration time:
-
Permalink:
Midnighter/httpx-limiter@716e64f4f84e585bcddf1cb3f19929360df809e9 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/Midnighter
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@716e64f4f84e585bcddf1cb3f19929360df809e9 -
Trigger Event:
push
-
Statement type: