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.2.tar.gz (66.0 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.2-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: premier-0.4.2.tar.gz
  • Upload date:
  • Size: 66.0 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.2.tar.gz
Algorithm Hash digest
SHA256 0d6d6de5b341fd1f9b0826eba7d56379dc3729082a2a17c6e99c0f3d9aa161d9
MD5 29b44f9cbae06721f9ca3f3f5d73b00f
BLAKE2b-256 a7ebb55215093887f23619f65556e22978de59d03afd57ef9a88e01b67dc0799

See more details on using hashes here.

File details

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

File metadata

  • Download URL: premier-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 17.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6aef78e2d2549a28afbc326cf9a559be01cc363599225c77de05aa32c9877da4
MD5 841810e66e4af4c92ac91f73aa6bb67e
BLAKE2b-256 255dfb9aee4f3970475ed9b74d698108d8b58221836deb0e9cc23ae14655f476

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