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
from premier.providers.redis import AsyncRedisCacheAdapter

REDIS_URL = "redis://@127.0.0.1:6379/0"
aredis = AsyncRedisCacheAdapter(AIORedis.from_url(REDIS_URL)) 

throttler = Throttler(
    handler = AsyncRedisHandler(aredis), # only if you need to throttle async functions
    algo=ThrottleAlgo.FIXED_WINDOW, # use fix window as the default throttling algorithm
    keyspace="premier:throttler", # set premier as the keyspace
)
  • use in lihil

checkout lihil-plugins for details

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.5.tar.gz (74.3 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.5-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: premier-0.4.5.tar.gz
  • Upload date:
  • Size: 74.3 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.5.tar.gz
Algorithm Hash digest
SHA256 d2893049059f8897f75ca724249f94d42a4d97186088b0efa57d102e3599e968
MD5 a51476b174ad92f21ede1ceeecb3e5d7
BLAKE2b-256 3183bb92a460edf5a252bb2fa78467094d8d55abb9a0b911795d38405dc1c050

See more details on using hashes here.

File details

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

File metadata

  • Download URL: premier-0.4.5-py3-none-any.whl
  • Upload date:
  • Size: 21.0 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d568abfbc9fb4b9efcde6a051866422bcd6e57aa1f27c7f8498d9282119e9df3
MD5 9cbaa65b978dfb48f678cfd85fc0009c
BLAKE2b-256 8db6f2fe8cc982d81a61e53b47c7803f0eb47055dbcef1d78d16acb7d5d84365

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