Skip to main content

an extendable toolbox for scalable apis

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

  • decorate functions to be throttled
import httpx
from premier import limits, throttler, ThrottleAlgo, RedisHandler

@throttler.fixed_window(quota=3, duration=5)
def request(url: str) -> str:
    r = httpx.get(url)
    return r.text

@throttler.token_bucket(quota=3, duration=5)
async def async_request(client: httpx.AsyncClient, url: str) -> str:
  r = await client.get('https://www.example.com/')
  return r.text
  • config throttler as your app starts
from redis import Redis
from redis.asyncio.client import Redis as AIORedis

REDIS_URL = "redis://@127.0.0.1:6379/0"
redis = Redis.from_url(REDIS_URL)
aredis = AIORedis.from_url(REDIS_URL) # only if you need to throttle async functions

throttler.config(
    handler = RedisHandler(redis=redis),
    aiohandler = AsyncRedisHandler(aredis), # only if you need to throttle async functions
    algo=ThrottleAlgo.FIXED_WINDOW, # use fix window as the default throttling algorithm
    keyspace="premier", # set premier as the keyspace
)
  • use in fastapi
@app.get("/", dependencies=[Depends(throttler.get_countdown)])
async def index():
    return {"msg": "Hello World"}

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=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

[optional]

  • redis >= 5.0.3

DevPlan

  • implement timeout feature
  • implement retry feature
  • implement cache feature

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.3.tar.gz (74.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

premier-0.4.3-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: premier-0.4.3.tar.gz
  • Upload date:
  • Size: 74.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for premier-0.4.3.tar.gz
Algorithm Hash digest
SHA256 77851e44bb6af754fc6c374419fa7f3c3a93d875e6a5c05cf850a45692848ac8
MD5 ad11810f0d73bbdc826cca9b8dfd52fe
BLAKE2b-256 5c18d7cfda47eb70903e44e375a540ed7c6b20fbf53a9db32129b470250b4964

See more details on using hashes here.

File details

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

File metadata

  • Download URL: premier-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for premier-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 35be1714708a5a7d72917020f3b2a002389af94ab7818503ab25523cb0f7c762
MD5 95e69d83d03b48bc5cec4d57a1885c5a
BLAKE2b-256 5d68628db13badfdd63d7dedc049d54f0ba2c428b2ca3e7ea51d70fec9c4c809

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page