Skip to main content

Python Rate-Limiter using Leaky-Bucket Algorimth Family

Project description

PyrateLimiter

The request rate limiter using Leaky-bucket algorimth

PyPI version Python 3.7 Maintenance PyPI license HitCount


Introduction

This module can be used to apply rate-limit for API request, using leaky-bucket algorimth. User defines window duration and the limit of function calls within such interval.

  • To hold the state of the Bucket, you can use LocalBucket as internal bucket.
  • To use PyrateLimiter with Redis, redis-py is required to be installed.
  • It is also possible to use your own Bucket implementation, by extending AbstractBucket from pyrate_limiter.core

Installation

Using pip/pipenv/poetry, whatever that works for your

$ pip install pyrate-limiter

API

One of the most pleasing features of this lib is that it is meant to be very extensible. People's efforts to solve rate-limiting problem has so far led to the introductions of few variations of the leaky-bucket algorimth. The idea behind this is you can extend the main core data-structure that power every member of this algorimth family.

AbstractBucket

from pyrate_limiter.core import AbstractBucket

AbstractBucket is a python abstract class that provide the Interface for, well, a queue. The algorimths provided in pyrate_limiter.core all make use of this data-structure. A solid implementation of this abstract class must includes 4 methods of the bucket instance.

class AbstractBucket(ABC):
    """An abstract class for Bucket as Queue"""

    __values__ = []

    @abstractmethod
    def append(self, item) -> None:
        """Add single item to the queue
        """
    @abstractmethod
    def values(self) -> List:
        """Return queue values
        """
    @abstractmethod
    def update(self, new_list: List) -> None:
        """Completely replace the existing queue with a new one
        """
    def getlen(self) -> int:
        """Return the current queue's length
        """
        return len(self.__values__)

Due to personal needs, 2 ready-use implementations with Redis and Application Local State are provided.

When designing a rate-limiting service that depends on different type of data-store, like Postgres or Mysql, user can write his own AbstractBucket implementation that fits their needs.

Usage

from pyrate_limiter.core import TokenBucketLimiter, LeakyBucketLimiter
from pyrate_limiter.engines.redis import RedisBucket
from pyrate_limiter.engines.local import LocalBucket
from pyrate_limiter.exceptions import BucketFullException

# Init redis bucket
bucket = RedisBucket('redis-url', hash='some-hash', key='some-key')

# Create Limiter using Token-Bucket Algorimth
# Maximum 10 items over 60 seconds
limiter = TokenBucketLimiter(bucket, capacity=10, window=60)
limiter.queue.config(key='change-key')
# Process an item
try:
    limiter.process('some-json-serializable-value')
    print('Item allowed to pass through')
except BucketFullException:
    print('Bucket is full')
    # do something



# Similarly, using Leaky-Bucket Algorimth
limiter = LeakyBucketLimiter(bucket, capacity=5, window=6)
limiter.queue.config(key='change-key')
# Process an item
try:
    # For LeakyBucketLimiter using the similar process method, only
    # different in naming...
    limiter.append('some-json-serializable-value')
    print('Item allowed to pass through')
except BucketFullException:
    print('Bucket is full')
    # do something


# If using LocalBucket, the instantiation is even simpler
bucket = LocalBucket(initial_values=some_list_type_value)

Understanding the Algorimths

View tests/test_leaky_bucket.py and tests/test_token_bucket.py for explaination. Documents are on the way.

LeakyBucket with Sliding-Window Algorimth

LeakyBucket with Sliding-Window Algorimth is a capped bucket of items. Every item expires after {window} time, making room for later items to go in.

Item's expiring-rate is {window} time. Using a simple timeline model, we can describe it as follow

TIME <<----------[===========WINDOW===========]--------------------------------<<
REQS >>--- <req> ---- <req> ---- <req> ---- <req> ---- <req> ---- <req> ------->>

TokenBucket

TokenBucket with Fixed-Window Algorimth can be described as multiple groups of Going-In-Items that does not exceed the Bucket Capacity running into the Bucket at fixed-interval between groups.

Bucket's queue reset if interval between 2 items is larger or equal {window} time.

>>-- [x items] ----- (window) ------ [y items] ------ (window) ------ [z items] --->>
eg:  3reqs/3s         <5sec>          2reqs/1s         <5sec>          3reqs/3s

Testing

Simple as it should be, given you have poetry installed...

$ poetry run test

CICD flow is not currently setup since I dont have much time, but FYI, the coverage is decent enought IMO...

tests/test_leaky_bucket.py::test_bucket_overloaded PASSED
tests/test_leaky_bucket.py::test_bucket_cooldown PASSED
tests/test_local_engine.py::test_invalid_initials PASSED
tests/test_local_engine.py::test_leaky_bucket_overloaded PASSED
tests/test_local_engine.py::test_leaky_bucket_cooldown PASSED
tests/test_local_engine.py::test_token_bucket_overloaded PASSED
tests/test_local_engine.py::test_token_bucket_cooldown PASSED
tests/test_redis_engine.py::test_bucket_overloaded PASSED
tests/test_redis_engine.py::test_bucket_cooldown PASSED
tests/test_redis_engine.py::test_normalize_redis_value PASSED
tests/test_redis_engine.py::test_token_bucket_overloaded PASSED
tests/test_redis_engine.py::test_token_bucket_cooldown PASSED
tests/test_token_bucket.py::test_bucket_overloaded PASSED
tests/test_token_bucket.py::test_bucket_cooldown PASSED

---------- coverage: platform darwin, python 3.7.5-final-0 -----------
Name                                Stmts   Miss  Cover
-------------------------------------------------------
pyrate_limiter/__init__.py              1      0   100%
pyrate_limiter/basic_algorimth.py      45      0   100%
pyrate_limiter/core.py                 63      3    95%
pyrate_limiter/engines/local.py        14      0   100%
pyrate_limiter/engines/redis.py        33      1    97%
pyrate_limiter/exceptions.py            5      0   100%
-------------------------------------------------------
TOTAL                                 161      4    98%

License

Copyright 2019 vutr

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

pyrate-limiter-1.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

pyrate_limiter-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pyrate-limiter-1.0.0.tar.gz.

File metadata

  • Download URL: pyrate-limiter-1.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.5 Darwin/18.7.0

File hashes

Hashes for pyrate-limiter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 299fe59c557fedec1c4ede3ddd13bf048ecb8683bfe62c2167a218b33f3d4d9b
MD5 6adc28bc2219889788c7b9bc91df1304
BLAKE2b-256 3d42f48f013bdb7f4c502a014a8e486bf18b9315915651e4b491512066a54749

See more details on using hashes here.

Provenance

File details

Details for the file pyrate_limiter-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyrate_limiter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.5 Darwin/18.7.0

File hashes

Hashes for pyrate_limiter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1236d8b5aaa0e89faadf673114a4aee9627725134d5450b9835e9d483b48f815
MD5 13da39529834b0c4565a3426c64f2ec7
BLAKE2b-256 6355e0a53874800a313c621cf366b1b10fa858edb9cc1d651496441c37fd4a40

See more details on using hashes here.

Provenance

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