Skip to main content

rrl - Redis Rate Limiting

Very simple reusable redis-backed rate limiting code. Designed for OpenStates.org.

Test

Configuration

rrl, as the name implies, requires a Redis server.

rrl is entirely configured by environment variables:

RRL_REDIS_HOST - hostname of Redis instance (default: localhost) RRL_REDIS_PORT - port of Redis instance (default: 6379) RRL_REDIS_DB - database ID to use for RRL (default: 0)

Usage

Usage can be throttled on a per-minute, per-hour, and/or per-day basis.

rrl has the concept of a Tier which associates a name with a set of limitations, for instance:

# create two tiers
bronze = Tier(name="bronze", per_minute=1, per_hour=0, per_day=500)
silver = Tier(name="silver", per_minute=5, per_hour=0, per_day=4000)

These tiers do not use the per_hour feature, but will limit users to 1 or 5 requests per minute respectively. There's also a daily limit at 500 or 4000 requests per day.

Then you'll need an instance of rrl.RateLimiter, which will be instantiated with these tiers:

limiter = RateLimiter(tiers=[bronze, silver])

Then to apply limiting, you'll call the check_limit function, which takes three parameters:

  • key - A unique-per user key, often the user's API key or username. (Note: rrl does not know if a key is valid or not, that validation should be in your application and usually occur before the call to check_limit.)
  • tier_name - The name of one of the tiers as specified when instantiating the RateLimiter class. (Note: rrl does not have a concept of which users are in which tier, that logic should be handled by your key validation code.)

Example call:

limiter.check_limit(key="1234", tier_name="bronze")

This call will return without any error if the call is deemed allowed.

If any of the rate limits are exceeded it will instead raise a RateLimitExceeded exception describing which limit was exceeded.
If multiple limits were exceeded it will return the shortest limit violated.

Advanced Usage

Obtaining Usage Information

Your RateLimiter instance also has a method named get_usage_since, which takes four parameters:

  • key - Which key you're requesting usage information for.
  • start - Date that you'd like usage since, as a datetime.date object.
  • end - Optional end date if you'd only like usage within a certain window, otherwise the current day is used.

This will return a list of DailyUsage dataclasses with the following attributes:

  • date - datetime.date
  • calls - Number of calls made on that date.

This method can be useful for showing users an overview of their data.

Advanced Configuration

When instantiating a RateLimiter there are several keyword-only parameters you may set:

prefix

Passing a prefix like:

limiter = RateLimiter(tiers, prefix="v1")

will scope all calls to limiter to a given prefix, this can be useful if you want multiple limiters but want to ensure that they do not interfere with one another.

use_redis_time

True by default, but if you set to False the application's system time will be used instead.

The tradeoff here is one fewer call to Redis per call to check_limit, but if your machines experience any clock drift unexpected results may occur.

track_daily_usage

True by default, but if set to False, rrl will not store the necessary information to make get_usage_since work. This results in a slight overhead reduction, but the usage information will not be stored in Redis and will be impossible to retrieve.

Metadata

Release files for rrl 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rrl 0.3.1
File Size Uploaded
rrl-0.3.1.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rrl 0.3.1
File Interpreter ABI Platform
rrl-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / rrl-0.3.1.tar.gz

Download URL rrl-0.3.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cfbeb818198bb53c5a3c1cb8df44e2be6049d8484a589d848f678613bc0ecaa3
BLAKE2b-256 checksum
How to use checksums
ee232cee4c679108670430a3e56f5d9fe1b63caa57707120d178325dd23ec8b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.5 Darwin/18.7.0

Release files / rrl-0.3.1-py3-none-any.whl

Download URL rrl-0.3.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ac0f1373600ba2d1c4bce05938cd97122e21d8dad81a72ed2108f8f52abc97d
BLAKE2b-256 checksum
How to use checksums
d16a3c190e9f9afefa4151322457150b0eae0b3a5d4ce8b95db6f6ae9d9ab0cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.5 Darwin/18.7.0

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

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