Comments (5)
From a very basic evaluation I think only fixed window rate limiting might be achievable (which is fine) though it's still not clear to me how one could atomically increment a value.
I've personally only used etcd for simple distributed config and never for anything involving counters or collections (needed for the moving window strategy), so I'm probably not the best person to try and implement this feature.
Having said that, if you have some pseudo code examples how one might implement the strategies that limits supports, I'm happy to give it a crack myself.
from limits.
Looking at some stuff, I think you could do something like:
lock = client.lock(b'rate-limit-key')
await lock.acquire()
key = await client.get(b'rate-limit-key')
if key is None:
await client.put(b'rate-limit-key', value)
else:
await client.delete(b'rate-limit-key')
await client.put(b'rate-limit-key', value)
await lock.release()
The only problem I could see with this is that it uses locks, because etcd doesn't support atomic increments itself and any alternatives/fixes seem too complicated, especially in async or distributed environments.
from limits.
I've pushed a very basic implementation of fixed window / fixed window + elastic expiry with etcd for both sync/async (using python-etcd3 and aetcd).
I ended up using transactions instead of locks since that made more sense to me. Leases don't fully satisfy the requirements of implementing rate limits since a lease doesn't guarantee expiry at an exact time so some weird hacks had to be done to encode the expiry into the value stored in etcd instead of purely relying on the ttl returned by the lease (one non trivial example is trying to set a rate limit of n/1 second
which won't work by depending only on a lease to expire the counter in a default etcd deployment since the minimum lease duration is 2 seconds).
Official support for this might not happen immediately since I noticed both python-etcd3 and aetcd are not very well maintained and require some workarounds to consistently install across python 3.8-3.11.
Regardless, this is available in master now and I'll do a release soon that includes this.
from limits.
Available now in 3.0.0
from limits.
@VincentRPS upon running some benchmarks I realized the etcd implementation is much slower than the redis/memcached/mongodb counterparts - approximately 10x in some cases. These benchmarks are run on an untuned single node docker container (and I understand etcd works best when backed by high performance disk), but I'm wondering if there is any way to configure etcd in a development environment for testing to emulate optimal performance (maybe a ram disk or something?) or if there are some obvious tweaks to the code that can help (though even a simple get operation in my benchmarks is significantly slower).
from limits.
Related Issues (20)
- ssl_cert_reqs="required" is not allowing me to connect to Redis HOT 2
- Question: Does In-Memory Storage clear itself when limit expires or it will keep getting bloated? HOT 1
- Flaky unit test(s): time window HOT 20
- Relax of packaging dependency did not take effect? HOT 3
- Hit incorrectly returns True when cost higher than limit HOT 4
- RedisCluster does not work with password in uri HOT 3
- Plans to add support for DynamoDB HOT 3
- `pkg_resources` is deprecated HOT 11
- Shared ratelimit across multiple instances HOT 2
- Exponential / growing limits HOT 2
- change coredis to redis-py HOT 1
- Issue with implementing limits in fastapi HOT 5
- Unable to create `MovingWindowRateLimiter` for Asynchronous Redis Sentinel storage HOT 2
- Support redis cluster like aws Elasti Cache HOT 2
- Unnecessary dependency on setuptools HOT 1
- Fixed window strategy using mongodb storage not working HOT 6
- Ability to 'undo' last hit on exception response HOT 2
- Extend WindowStats: add time left before reset HOT 2
- Bug: async RedisStorage does not accept `async+unix` URIs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from limits.