-
-
Notifications
You must be signed in to change notification settings - Fork 179
RLWrapperTimeouts
It wraps any limiter by adding timeout functionality. If the wrapped limiter operation takes longer than the specified timeout, it will reject with an error.
const limiter = new RateLimiterValkey({
storeClient: valkeyClient,
points: 1,
duration: 1,
});
const limiterWrapped = new RLWrapperTimeouts({
limiter,
timeoutMs: 1000,
});If the operation takes longer than timeoutMs milliseconds, it will reject with "Operation timed out" error.
When a timeout occurs or the wrapped limiter fails, the wrapper will automatically fallback to the insuranceLimiter if it's set:
const primaryLimiter = new RateLimiterValkey({
storeClient: valkeyClient,
points: 1,
duration: 1,
});
const insuranceLimiter = new RateLimiterMemory({
points: 1,
duration: 1,
});
const limiterWrapped = new RLWrapperTimeouts({
limiter: primaryLimiter,
timeoutMs: 500,
insuranceLimiter: insuranceLimiter,
});If primaryLimiter times out or fails, it will automatically use insuranceLimiter.
If the wrapped limiter is also a RateLimiterInsuredAbstract (like RateLimiterStoreAbstract), the wrapper will automatically inherit its insuranceLimiter:
const limiter = new RateLimiterValkey({
storeClient: valkeyClient,
points: 1,
duration: 1,
insuranceLimiter: new RateLimiterMemory({
points: 1,
duration: 1,
}),
});
const limiterWrapped = new RLWrapperTimeouts({
limiter,
timeoutMs: 1000,
});limiterWrapped automatically has access to the insuranceLimiter from the wrapped limiter. If timeout occurs, it will use the insuranceLimiter.
Wrapped limiter has the same methods as any other limiter from this package.
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