Skip to main content

This is an implementation of GCRA for rate limiting based on Redis.

The code requires Redis version 3.2 or newer since it relies on replicate_commands feature.

How to Install?

pip install pyredis-rate-limiter

How to Use?

Here is a short example.

# -*- coding: utf-8 -*-
import asyncio
from typing import Optional

import redis as aio_redis
from loguru import logger as loguru_logger
from pyredis_rate_limiter import (
    Limit,
    Limiter,
    per_second
)

_rl_instance: Optional[Limiter] = None


def init_rl_instance(redis_conn: aio_redis.Redis):
    global _rl_instance
    _rl_instance = Limiter(redis_conn)


def rl_instance() -> Limiter:
    return _rl_instance


async def take_token(key: str, limit: Limit, block_wait: bool = False) -> Optional[int]:
    token: Optional[int] = None

    ret, _ = await rl_instance().aio_allow(key, limit)
    if ret is None:
        return token
    if ret.allowed == 0:
        if block_wait:
            wait_until_available = ret.retry_after_in_sec
            loguru_logger.warning(f"Token for key:{key} exceeds, wait {wait_until_available} secs until resource turns to be available")
            await asyncio.sleep(ret.retry_after_in_sec)
        else:
            loguru_logger.error(f"no token available for key:{key}")
    else:
        token = 1
    return token


async def send_sm():
    token = await take_token(key="send_sm_handler", limit=per_second(5))
    if token is None:
        # do sth
    else:
        # do sth

Metadata

Release files for pyredis-rate-limiter 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyredis-rate-limiter 1.0.1
File Size Uploaded
pyredis_rate_limiter-1.0.1.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyredis-rate-limiter 1.0.1
File Interpreter ABI Platform
pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 15.7 kB

Release files / pyredis_rate_limiter-1.0.1.tar.gz

Download URL pyredis_rate_limiter-1.0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e3de4bba7adb6d1bf0fdbf4e2d6117d38f66075d32259bad8fffe9c4d971d59c
BLAKE2b-256 checksum
How to use checksums
e2ff40e0666950558a26d2950312d15fa4c34070fbd2b0bdd3bdeba26802398e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.8.16

Release files / pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl

Download URL pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
20526fc8489890db6c1f7ae611a3fd4c27f0c12881dad896259a4570fc03b99e
BLAKE2b-256 checksum
How to use checksums
2c6ff845f9f825c7a2a54d811ae6f3b72fc143099d5b7031c185de3e53ba7015
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.8.16

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page