Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document semantics of rate limit window #134

Open
cpsnowden opened this issue Sep 27, 2023 · 0 comments
Open

Document semantics of rate limit window #134

cpsnowden opened this issue Sep 27, 2023 · 0 comments

Comments

@cpsnowden
Copy link

cpsnowden commented Sep 27, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant