Skip to main content

simple redis rate limiting

Project description

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.

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

rrl-0.3.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

rrl-0.3.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file rrl-0.3.1.tar.gz.

File metadata

  • Download URL: rrl-0.3.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Darwin/18.7.0

File hashes

Hashes for rrl-0.3.1.tar.gz
Algorithm Hash digest
SHA256 cfbeb818198bb53c5a3c1cb8df44e2be6049d8484a589d848f678613bc0ecaa3
MD5 6d8e5dbe15abb7fddf7a03cfa5672415
BLAKE2b-256 ee232cee4c679108670430a3e56f5d9fe1b63caa57707120d178325dd23ec8b8

See more details on using hashes here.

File details

Details for the file rrl-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: rrl-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.5 Darwin/18.7.0

File hashes

Hashes for rrl-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ac0f1373600ba2d1c4bce05938cd97122e21d8dad81a72ed2108f8f52abc97d
MD5 265a140918bebd78c3ce107c20263ee9
BLAKE2b-256 d16a3c190e9f9afefa4151322457150b0eae0b3a5d4ce8b95db6f6ae9d9ab0cd

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