-
-
Notifications
You must be signed in to change notification settings - Fork 173
Etcd
The rate limiter has been tested with the etcd version 3.5 and the etcd3 package version 1.1.
The rate limiter for the etcd store is offered in two different flavors - RateLimiterEtcd
and RateLimiterEtcdNonAtomic
.
As the etcd store does not offer an atomic operation to easily add a value to the actual rate, there is a non-atomic version of the etcd rate limiter which uses a combination of a simple get and put request on the etcd store. The update will be fast, but an atomic increment is not guaranteed on the RateLimiterEtcdNonAtomic
class.
The RateLimiterEtcd
class instead implements the _upsert
method by using a conditional write. This conditional write works like a transaction as follows:
- Build the new value
B
for the given key. - Get the old value
A
for the given key from the store - Conditionally write the new value
B
for the given key if and only if the key still holds the old valueA
. - If the conditional write fails go back to step #2 four times.
- If the conditional write has still not succeeded, bail out.
const { Etcd3 } = require('etcd3');
const RateLimiterEtcd = require('../lib/RateLimiterEtcd');
// First create the store client.
const etcdClient = new Etcd3({
hosts: 'http://localhost:2379',
});
// Then create the etcd limiter with the given store instance.
const rateLimiter = new RateLimiterEtcd({
storeClient: etcdClient,
points: 2,
duration: 5,
});
rateLimiter
.consume(testKey, 1)
.then((res) => {
// Apply some app logic
})
.catch((rateLimiterResOrError) => {
// Not enough points to consume or error happened
});
The same logic applies to the non-atomic rater limiter class RateLimiterEtcdNonAtomic
.
Get started
Middlewares and plugins
Migration from other packages
Limiters:
- Valkey Glide
- IoValkey
- Redis
- Memory
- DynamoDB
- Prisma
- Etcd
- MongoDB (with sharding support)
- PostgreSQL
- MySQL
- SQLite
- BurstyRateLimiter
- Cluster
- PM2 Cluster
- Memcached
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- RLWrapperBlackAndWhite Black and White lists
Knowledge base:
- Block Strategy in memory
- Insurance Strategy
- Periodic sync to reduce number of requests
- Comparative benchmarks
- Smooth out traffic peaks
-
Usage example
- Minimal protection against password brute-force
- Login endpoint protection
- Websocket connection prevent flooding
- Dynamic block duration
- Different limits for authorized users
- Different limits for different parts of application
- Block Strategy in memory
- Insurance Strategy
- Third-party API, crawler, bot rate limiting