Coder Social home page Coder Social logo

wemake-services / asyncio-redis-rate-limit Goto Github PK

View Code? Open in Web Editor NEW
34.0 3.0 2.0 1002 KB

Rate limiter for async functions using Redis as a backend.

Home Page: https://pypi.org/project/asyncio-redis-rate-limit/

License: MIT License

Makefile 1.30% Python 98.70%
aioredis python python3 redis redispy ratelimit aiohttp asyncio httpx rate-limit rate-limiter rate-limiting

asyncio-redis-rate-limit's Issues

Enable `wps` in actions

Right now for some reason poetry install with wps fails.
I think that it is pip / poetry problem. We need to add this later.

Document semantics of rate limit window

From reading the code, the following comment indicates that Redis Pattern: Rate Limiter 1 is being used which is confusing as the cache key does not contain the timestamp.

    async def _run_pipeline(
        self,
        cache_key: str,
        pipeline: AnyPipeline,
    ) -> int:
        # https://redis.io/commands/incr/#pattern-rate-limiter-1
        current_rate, _ = await pipeline_expire(
            pipeline.incr(cache_key),
            cache_key,
            self._rate_spec.seconds,
        ).execute()
        return current_rate

My understanding is that in-fact a variation of Redis Pattern: Rate Limiter 2 is being used as a by-product of the NX command.

Is this understanding correct? If so - would be useful to update the comment.

Attaching the reference patterns

Pattern 1

FUNCTION LIMIT_API_CALL(ip)
ts = CURRENT_UNIX_TIME()
keyname = ip+":"+ts
MULTI
    INCR(keyname)
    EXPIRE(keyname,10)
EXEC
current = RESPONSE_OF_INCR_WITHIN_MULTI
IF current > 10 THEN
    ERROR "too many requests per second"
ELSE
    PERFORM_API_CALL()
END

Pattern 2

FUNCTION LIMIT_API_CALL(ip):
current = GET(ip)
IF current != NULL AND current > 10 THEN
    ERROR "too many requests per second"
ELSE
    value = INCR(ip)
    IF value == 1 THEN
        EXPIRE(ip,1)
    END
    PERFORM_API_CALL()
END

Add support for redis:6.2

For redis:6.2 it raises the following error:

Command # 2 (EXPIRE my_key 1 NX) of pipeline caused error: ("wrong number of arguments for 'expire' command",)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.