-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Bfor the given key. - Get the old value
Afor the given key from the store - Conditionally write the new value
Bfor 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('rate-limiter-flexible');
// 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:
- Cluster
- Drizzle
- DynamoDB
- Etcd
- Memcached
- Memory
- MongoDB (with sharding support)
- MySQL
- PM2 Cluster
- PostgreSQL
- Prisma
- Redis
- SQLite
- Valkey: iovalkey and Valkey Glide
- BurstyRateLimiter
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- AWS SDK v3 Client Rate Limiter
- RLWrapperBlackAndWhite Black and White lists
- RLWrapperTimeouts Timeouts
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