Skip to main content

an intuitive throttler supports distributed usage and various throttling algorithms

Project description

premier

premier is an intuitive throttler that supports various backends and throttling algorihms, it can be used in distributed application for throttling web-api and any regular function.

Feature

  • Distributed throttling via redis or other backend.
  • Support asyncio mode
  • Support various throttling algorithms
  • Designed to be highly customizable and extensible.

Usage

  1. decorate functions to be throttled
from premier import limits, throttler, ThrottleAlgo

@throttler.fixed_window(quota=3, duration_s=5, algo=ThrottleAlgo.FIXED_WINDOW)
def add(a: int, b: int) -> int:
    res = a + b
    return res
  1. config throttler when app starts
redis = Redis.from_url("redis://@127.0.0.1:6379/0")
throttler.config(
    quota_counter=RedisCounter(redis=redis, ex_s=15), # set key expirey to 15 seconds
    algo=ThrottleAlgo.FIXED_WINDOW,# use fix window as the default throttling algorithm
    keyspace="premier", # set premier as the keyspace
)

Install

pip install premier

Advanced Usage

Keyspace

by default, premier creates keyspace of this format for throttled functions

{keyspace}:{module}:{funcname}:{algorithm}

name explain default
keyspace customized string provided by user "premier"
module module name where function is defined in func.__module__
funcname name of the function func.__name__
algorithm throttling algorithm of the function fixed_window

Customized throttle key

You might provide your own keymaker to the 'throttler' function like this

from premier import throttler

@throttler.fixed_window(quota=3, duration_s=5, keymaker=lambda a, b: f"{a}")
def add(a: int, b: int) -> int:
    res = a + b
    return res

Supported Backend

backend sync async
redis supported supported
memory supported supported

Supported Algorithms

algorithm status
fixed window supported
sliding window supported
leaky bucket supported
token bucket supported

requirements

  • python >= 3.10
  • redis >= 5.0.3

DevPlan

TODO:

  • support lowering version python by using type-extensions

  • implement timeout feature

  • implement retry feature

  • implement cache feature

API Design:

type Strategy = ty.Callable[[int], float]

@cache
@retry(strategy="expo", max=3, on_exception=(TimeOut, QuotaExceeds))
@timeout(60)
@throttler.leaky_bucket
def add(a:int, b:int):
    return a + b

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

premier-0.4.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

premier-0.4.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file premier-0.4.0.tar.gz.

File metadata

  • Download URL: premier-0.4.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.0 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for premier-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9a26564fc45dcca5118eeaf24fd487b50f654d5bf203da189d362830bd63dd0e
MD5 80cf763434d17354c950a7bbd11159a9
BLAKE2b-256 bd4817da7b5e00da6ed5dd1cc70a1569f6b99f6dd435c8613a50742f8439117e

See more details on using hashes here.

File details

Details for the file premier-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: premier-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.0 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for premier-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8550a18fd99ef6e73594bc94186d720037623e2e3900064bafeba4e38b0af5af
MD5 f2f9b328de8aba0c67c20a93914a8147
BLAKE2b-256 8540aba1ed39808e54f2d83f9c0d64e498c91a54e270f7490e3587e65ebadb8e

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