Skip to main content

Credit Rate Limiter

Easily rate limit async requests to API using credits, computation unit per second (CUPS) or request units, and to those just counting the number of requests per time unit


Project Information

Tests & Lint PyPI - Python Version GitHub release (latest by date) PyPi Repository GitHub

Code Quality

CodeQL Test Coverage Imports: isort Type Checker: mypy Linter: flake8


Overview

Some APIs enforce a rate limit based on credits (or computation unit per second (CUPS) or request units). Meaning the call to an endpoint may not weigh as the call to another one. For example, let's consider a "Compute Unit Costs" sample from an actual crypto related API:

Method Compute Units
eth_chainId 0
eth_blockNumber 10
eth_getTransactionReceipt 15
eth_getBalance 19
eth_call 26
eth_estimateGas 87
eth_sendRawTransaction 250

It is clear that calling some methods will impact your rate limit more than some others!

This library aims to provide an easy way to limit the rate at which an async function or method can be called, considering its own credit cost and the credits already used for a given period. It also supports rate limitation just based on number of calls. Finally, you can "group" calls so requesting one API does not impact the rate limit of another one.

It works well for any request pace, but especially well if there are some bursts of fast requests, and it can be optimized for even better performances.

Installation

pip install credit-rate-limit

Usage

This library provides 2 "Rate Limiters":

  • CreditRateLimiter: for APIs that use credits, computation unit per second (CUPS), request units ...
  • CountRateLimiter: for APIs that just counts the number of calls per time unit.

Once the "Rate Limiter" is built, you just have to add the decorator throughput to the functions or methods you wish to limit.

Examples

from credit_rate_limit import CreditRateLimiter, throughput

credit_rate_limiter = CreditRateLimiter(200, 1)  # the API allows 200 credits per 1 second

@throughput(credit_rate_limiter, request_credits=40)  # this function costs 40 credits to call
async def request_api():
    ...

If you wish to define a "Rate Limiter" as an object attribute, just gives its name as a str to the decorator:

from credit_rate_limit import CreditRateLimiter, throughput

class MyClass:
    def __init__(self):
        self.my_credit_rate_limiter = CreditRateLimiter(200, 1)

    # @throughput(self.my_credit_rate_limiter, request_credits=40)  /!\ Error: self is unknown here !!
    @throughput(attribute_name="my_credit_rate_limiter", request_credits=40)
    async def request_api(self):
        ...

CountRateLimiter can be used in the same way, albeit without request_credits. For example:

from credit_rate_limit import CountRateLimiter, throughput

credit_rate_limiter = CountRateLimiter(5, 1)  # the API allows 5 requests per 1 second


@throughput(credit_rate_limiter)
async def request_api():
    ...

Optimization

Both CreditRateLimiter and CountRateLimiter have the adjustment parameter that can be used to speed up the requests (to some extent)
It can take any value between 0 (default) and interval. A higher value means better performances, but also a higher risk of being rate limited by the API.
There is no right value: it depends on the API, the request speeds and the network state and would be discovered with tests. But if this parameter is correctly set, maybe with a re-try mechanism, it may give a nice performance improvement in some use cases.

rate_limiter = CreditRateLimiter(max_credits=2000, interval=1, adjustment=0.1)

Metadata

Release files for credit-rate-limit 0.2.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 credit-rate-limit 0.2.0
File Size Uploaded Provenance Actions
credit_rate_limit-0.2.0.tar.gz 7.0 kB Without provenance Details for credit_rate_limit-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for credit-rate-limit 0.2.0
File Interpreter ABI Platform Actions
credit_rate_limit-0.2.0-py3-none-any.whl Python 3 none any Details for credit_rate_limit-0.2.0-py3-none-any.whl

Total release size: 13.6 kB

Release files / credit_rate_limit-0.2.0.tar.gz

Download URL credit_rate_limit-0.2.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
85c3363f0439a5a3f183ce64ae38eb12326c7f42ec641ebabd27b91ecf907abd
BLAKE2b-256 checksum
How to use checksums
20126918c98ba203647b50252a70b1164fd2993e3242329218bac95fb145d355
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.5

Release files / credit_rate_limit-0.2.0-py3-none-any.whl

Download URL credit_rate_limit-0.2.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0bd5d27f90bc3d41cb7a4eb5341a8fced33e09c38f71e1ffde2dc16c24a3488
BLAKE2b-256 checksum
How to use checksums
60d050f8c45b560583ddb92daab94fdf256ee23156ef4f822e191909c424da95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.5

Security for credit-rate-limit 0.2.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