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.0.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.0-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.0.tar.gz.

File metadata

  • Download URL: pyredis_rate_limiter-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 dd0bf4bc93ffad72ed9493b86c4b9845cf70a1ad570c2651726ec18c5daf5492
MD5 2ff9577842c3ba2ff0b4823dfbcfe00f
BLAKE2b-256 079c15b49132ae58c348f77c8e348744a40e56921ac5bd823a2e18c3e6ecc92c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyredis_rate_limiter-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3d657554e9068eed9d975bac260942ba9ed8869d1818349c9252bcb43a8af291
MD5 bbec58dd5b7e6f82036cdbe225685f4f
BLAKE2b-256 5ed10b612265d5187a40a3f414a9fc9e0298388e55f6155d6e048b90d48d5604

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