Skip to main content

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

Project description

Credit Rate Limiter

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

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.

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 a class 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():
    ...

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

credit_rate_limit-0.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

credit_rate_limit-0.0.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file credit_rate_limit-0.0.0.tar.gz.

File metadata

  • Download URL: credit_rate_limit-0.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.5

File hashes

Hashes for credit_rate_limit-0.0.0.tar.gz
Algorithm Hash digest
SHA256 80b98dd2d221038fbdd34b9225f03ffce89f98b875f8b3e4effdf2ac4c0d6cc6
MD5 b8b2da6b0f335841e76601159cdbadc6
BLAKE2b-256 5996991f4864f8e8398ba23593f743e400354555fbfe69b48e92577dad5a9da5

See more details on using hashes here.

File details

Details for the file credit_rate_limit-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for credit_rate_limit-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cc88ca9853e707a00d785819d039efd5732cbdf96000ab77b74b7e43061c45e
MD5 597d825cc44aa2bfdc03a12c937e05eb
BLAKE2b-256 2aa01b1e0621ff815e996ef4489594b3f5b67cb76ad062923e566d4c4d56a3b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page