Skip to main content

A Redis-backed rate limiting based on GCRA implementation in Python

Project description

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

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

pyredis_rate_limiter-1.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyredis_rate_limiter-1.0.1.tar.gz.

File metadata

  • Download URL: pyredis_rate_limiter-1.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.16

File hashes

Hashes for pyredis_rate_limiter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e3de4bba7adb6d1bf0fdbf4e2d6117d38f66075d32259bad8fffe9c4d971d59c
MD5 50f398ff686c56c36ca50fb0b589253d
BLAKE2b-256 e2ff40e0666950558a26d2950312d15fa4c34070fbd2b0bdd3bdeba26802398e

See more details on using hashes here.

File details

Details for the file pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pyredis_rate_limiter-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 20526fc8489890db6c1f7ae611a3fd4c27f0c12881dad896259a4570fc03b99e
MD5 237b80f484456eb30bcad5fc0c1a29af
BLAKE2b-256 2c6ff845f9f825c7a2a54d811ae6f3b72fc143099d5b7031c185de3e53ba7015

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