Skip to main content

keylimiter - A Thread-safe rate limiter for Python

PyPI version License: MIT License: MIT

Two of the most popular rate limiter algorithms: token bucket and sliding window counter.

it's "key"limiter to enphasize that it's a key based rate limiter, not a ip based rate limiter, so the key can be anything, like an ip, an user id, an url, etc.

Some use cases:

  • limit the number of requests per ip
  • limit likes per user in a post
  • limit the number of notifications per user

token bucket

SOME_IP = "111.222.1.1"

"""
:param int bucket_size: maximum number of tokens in the bucket
:param float refill_rate: token refill rate in seconds
:param Callable[[], float] time_func: function that returns the current time in seconds. Default: monotonic
"""
limiter = TokenBucketLimiter(bucket_size=3, refill_rate=1)

for _ in range(3):
    assert limiter.allow(SOME_IP) == True
    
assert limiter.allow(SOME_IP) == False

token bucket

sliding window counter

SOME_IP = "111.222.1.1"

"""
:param int max_requests: maximum number of requests in a window
:param str window_unit: "second" | "minute" | "hour". Default: "minute"
:param Callable[[], float] time_func: function that returns the current time in seconds. Default: monotonic
"""
limiter = SlidingWindowCounterLimiter(max_requests=3, window_unit="second")

for _ in range(3):
    assert limiter.allow(SOME_IP) == True
    
assert limiter.allow(SOME_IP) == False

sliding window counter

Metadata

Release files for keylimiter 0.1.5

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

Source distribution (sdist)

Source distribution for keylimiter 0.1.5
File Size Uploaded
keylimiter-0.1.5.tar.gz 16.3 kB Details

Built distribution (wheel)

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

Total release size: 35.2 kB

Release files / keylimiter-0.1.5.tar.gz

Download URL keylimiter-0.1.5.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6655240c15d1aee6acf26bd66bb1ae6c5dfd014e287f9c9c563dcfeacfc8fc81
BLAKE2b-256 checksum
How to use checksums
dfe2d3da04a3ce3a28d9553c3f0bc646abb345fd106c64e8e1cdb61fb4887161
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / keylimiter-0.1.5-py3-none-any.whl

Download URL keylimiter-0.1.5-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08d0f7b05133e3e2b7f7aab8e023a8881deb37ec213a3ff270d79876658ed2e5
BLAKE2b-256 checksum
How to use checksums
ea1120ecbfd26ea2f0715b8cd0308d2d1a6e3401f7395162a3fd4c7226b4b2a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

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