Skip to main content

A CLI to manage rate limits.

Project description

Adding rate limits to your API

This is a python CLI that communicates with a cloud based HTTP Ratelimit Service to configure rate limits based on the token bucket algorithm.

Redis is used as the backing storage for the rate limit configurations, and using Lua scripts, the Ratelimit Service can make a determination to allow or deny a request within milliseconds.

This python package also comes with:

  1. An HTTP client that can be used to manually test your limits.
  2. A python decorator for your HTTP endpoint functions.

Where to start

The quickstart below will guide you through configuring the CLI on your system, authenticating to the Ratelimit Service, creating a rate limit, and testing it.

For more detailed documentation check out https://docs.ratelimit.xyz.

Configure the CLI

  1. Install the RateLimit CLI.
$ pip install ratelimitcli
  1. Configure the RateLimit CLI and follow the interactive prompts. You won't have an API key yet so you can just press [ENTER] when asked for a value.
$ ratelimitcli configure
Client ID  [None]:
API key    [None]:
First name [None]: Jasmin
Last name  [None]: Smith
Email name [None]: jasmin.smith@gmail.com
Updated config file (~/.ratelimit/config).
  1. Request an API key and a Client ID. You'll be asked to enter credit card information.
$ ratelimitcli billing configure
Enter a credit card number: 1234567890123456
Enter the expiration date for that credit card: 10/12
Enter the CCV for that credit card: 333
New values have been written to the config file.

Disclaimer

I have not yet implemented payments and as such, you won't be charged for anything. That said, this service is currently in a closed alpha. There is only a single credit card that will allow you to get an API key and you'd have to get that from me.

If this service or the concept is interesting enough for you and you don't mind sending an email, feel free to contact me and we can discuss your usecase.

Oh and the fake card I show above won't work - just FYI.

  1. Check to see that the config file has been written to $HOME/.ratelimit/config
$ cat ~/.ratelimit/config

The CLI is now configured and ready to use.


Creating your first limit

  1. Create your first limit by supplying integers for the burst limit and the rate limit.
$ ratelimitcli limits upsert \
  --throttling-burst-limit 2 \
  --throttling-rate-limit 0

Burst Limit

The maximum available instantaneous capacity.
https://en.wikipedia.org/wiki/Token_bucket

Rate Limit

The rate at which used capacity is restored.
https://en.wikipedia.org/wiki/Token_bucket

You'll know everything is set up when you get an API response with a limit ID that identifies the rate limit you just created.

Returns

Ok: API response ({"limit_id": "a9f9f31b-2c0b-321b-b398-f9d36kd30820"}).
  1. We can store that limit ID into an environment variable for ease of access.
export LIMIT_ID=a9f9f31b-2c0b-321b-b398-f9d36kd30820
  1. Test your first rate limit.
# ok - capacity 2, using the env var
$ ratelimitcli limits record $LIMIT_ID

# ok - capacity 1, using ID directly
$ ratelimitcli limits record \
  a9f9f31b-2c0b-321b-b398-f9d36kd30820

# ERROR - capacity 0, we've exhausted the capacity
$ ratelimitcli limits record $LIMIT_ID
  1. Test rate limits in a python interpreter shell. Note that in production, you most likely will not be using the RatelimitClient directly, nor will you be using synchronous calls to the Ratelimit Service.
$ python

# Get the limit ID from the env var
>>> import os
>>> limit_id = os.environ['LIMIT_ID']
>>> limit_id
'a9f9f31b-2c0b-321b-b398-f9d36kd30820'

# Import the client
>>> from ratelimitcli.client.client import RatelimitClient

# All args are optional.
# Defaults are pulled from env and config file.
>>> client = RatelimitClient()

# Synchronous calls to the service.
>>> client.sync_record_request(limit_id)
>>> client.sync_record_request(
... "a9f9f31b-2c0b-321b-b398-f9d36kd30820"
... )
  1. Use your rate limit in your code.
from fastapi import FastAPI

from ratelimitcli.client.exceptions import APIRateLimitException
from ratelimitcli.client.decorator import RatelimitDecorator

app = FastAPI()

def on_error_callback(
  msg: str,
  exception: APIRateLimitException
):
    """Callback function.

    Args:
        1. All the `*args` and `**kwargs` that were passed
           to the original function. In this case `msg: str`.

        2. The exception that was raised by RatelimitDecorator

    Returns:
        Some value that takes the place of the wrapped function's
        return value.
    """
    return "Goodbye, World!" + msg


@app.get("/")
@RatelimitDecorator(
    # The limit ID to check capacity against.
    id="a9f9f31b-2c0b-321b-b398-f9d36kd30820",
    # The function to call if capacity has been exhausted.
    callback=on_error_callback,
)
async def hello(msg: str):
    return "Hello, World!" + msg

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

ratelimitcli-0.2.2.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

ratelimitcli-0.2.2-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file ratelimitcli-0.2.2.tar.gz.

File metadata

  • Download URL: ratelimitcli-0.2.2.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.2 Darwin/21.5.0

File hashes

Hashes for ratelimitcli-0.2.2.tar.gz
Algorithm Hash digest
SHA256 60c4ef43be7418620986395e0416e4f15abcbf8279b11d7fa2c351f399268f65
MD5 46c23d7c014ae4b857d3326e44568812
BLAKE2b-256 7d89bb3b23632d573776e103a2dcf93e15b6fa867cc10a30381a0c0920c35ad2

See more details on using hashes here.

File details

Details for the file ratelimitcli-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: ratelimitcli-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.2 Darwin/21.5.0

File hashes

Hashes for ratelimitcli-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2b0d77263c7bf7ef2785b9d59ea76da0bf01439949628ce6c659ddd2a9f850
MD5 a4523fe65e7773ea912becb26a546d1d
BLAKE2b-256 a833bf1b90787150ae8067d8696b4e2f311213beb47886ff6a43c362e1e531f1

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