Leak Snek is a Python library that provides a flexible and extensible implementation of rate limiting for your applications
Project description
Leak Snek Rate Limiting
Leak Snek is a Python library that provides a flexible and extensible implementation of rate limiting for your applications
Table of Contents
Features
- Asynchronous and Synchronous Support: The library provides support for both asynchronous and synchronous rate.
- Flexible Rate Limiting Algorithms: Define your custom rate limiting algorithms by implementing the
RateLimiter
andAsyncRateLimiter
interfaces. - Rate Storage: Manage rate information with the
RateStorage
andAsyncRateStorage
interfaces. - Leaky Bucket Algorithm: Included implementations of the Leaky Bucket Algorithm for both asynchronous and synchronous use cases.
Getting Started
-
Installation:
Install Leak Snek using pip:
pip install leak_snek
-
Usage:
You can use the provided classes and interfaces to implement rate limiting in your Python applications. Additionally, if you need the Leaky Bucket Algorithm, you can use the provided implementations.
-
Synchronous Leaky Bucket Algorithm:
from threading import Lock from leak_snek.limiters.leaky_bucket import LeakyBucketLimiter from leak_snek.mutexes.memory_mutex import MemoryMutex from leak_snek.shortcuts.rate_limit import rl from leak_snek.storages.memory_storage import MemoryStorage limiter = LeakyBucketLimiter[str]( rate_limit=rl("10/m"), rate_storage=MemoryStorage(), key_mutex=MemoryMutex( local_lock=Lock(), lock_factory=lambda: Lock(), ), ) if not limiter.limit_exceeded("my_key"): ... # Perform the operation
-
Asynchronous Leaky Bucket Algorithm:
from collections.abc import AsyncGenerator, Awaitable from contextlib import asynccontextmanager from typing import Any, Self, cast, override from redis.asyncio import Redis from leak_snek.interfaces.mutexes.aio.mutex import AsyncMutex from leak_snek.interfaces.storages.aio.rate_store import AsyncRateStorage from leak_snek.interfaces.values.rate import Rate from leak_snek.limiters.aio.leaky_bucket import AsyncLeakyBucketLimiter from leak_snek.shortcuts.rate_limit import rl @dataclasses.dataclass class RedisAsyncRateStorage(AsyncRateStorage[str]): redis: Redis async def read(self: Self, key: str) -> Rate: if not await self.redis.exists(key): return Rate.default() rate_dict = await cast(Awaitable[dict[Any, Any]], self.redis.hgetall(name=key)) return Rate(operations=int(rate_dict[b"operations"]), updated_at=float(rate_dict[b"updated_at"])) async def write(self: Self, key: str, value: Rate) -> None: await cast( Awaitable[int], self.redis.hset(name=key, mapping={"operations": value.operations, "updated_at": value.updated_at}), ) @dataclasses.dataclass class RedisAsyncMutex(AsyncMutex[str]): redis: Redis @override @asynccontextmanager async def lock(self: Self, key: str) -> AsyncGenerator[None, None]: async with self.redis.lock(f"{key}_lock"): yield async def main() -> None: async_limiter = AsyncLeakyBucketLimiter( rate_limit=rl("10/m"), rate_storage=RedisAsyncRateStorage(redis=Redis()), key_mutex=RedisAsyncMutex(redis=Redis()), ) if not await async_limiter.limit_exceeded("my_key"): ... # Perform the operation ```
-
In asynchronous example we implement our own async storage & mutex b/c leak_snek doesn't provide redis integration and it doesn't make much of a sense to implement asynchronous in-memory store.
License
Leak Snek Rate Limiting is released under the MIT License. See the LICENSE file for details.
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
Built Distribution
Hashes for leak_snek-0.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f34ca3a35231dfe8cd50b51843de99f53e4a284cd4b606cd9fa33d5fc6a5f238 |
|
MD5 | 55b3678b8995611a7bfeae5a9c828484 |
|
BLAKE2b-256 | b26dd6e6a0a94a687cdb8c223383763849838dcb97c693dc29a6889e180ad828 |