wemake-services / asyncio-redis-rate-limit Goto Github PK
View Code? Open in Web Editor NEWRate limiter for async functions using Redis as a backend.
Home Page: https://pypi.org/project/asyncio-redis-rate-limit/
License: MIT License
Rate limiter for async functions using Redis as a backend.
Home Page: https://pypi.org/project/asyncio-redis-rate-limit/
License: MIT License
Right now for some reason poetry install
with wps
fails.
I think that it is pip
/ poetry
problem. We need to add this later.
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",)
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
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
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
For some reason, it is failing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.