Using Event Emitter
Leverage the built-in Node.js EventEmitter in oss-ratelimit to subscribe to events like 'limitReached' or 'error' for monitoring, logging, or custom logic.
Events
The Ratelimit instance and the RateLimitBuilder (registry) are EventEmitters.
Ratelimit Instance Events:
error: Emitted when an operational error occurs (e.g., Redis issue). Passes the error object.allowed: Emitted when a.limit()call succeeds. Passes{ identifier, remaining, limit }.limited: Emitted when a.limit()call fails due to rate limiting. Passes{ identifier, remaining, limit }.failOpen: Emitted when Redis fails butfailOpen: trueallows the request. Passes{ identifier, error }.waiting: Emitted byblock()on each retry attempt. Passes{ identifier, attempt, waitTime, elapsed }.reset: Emitted when.reset()successfully clears a limit. Passes{ identifier }.
Registry (RateLimitBuilder) Events:
redisConnect: Emitted when a managed Redis client connects successfully. Passes{ clientKey }.redisError: Emitted when a managed Redis client encounters an error. Passes{ clientKey, error }.limiterRegister: Emitted when a limiter instance is successfully created and registered. Passes{ name, clientKey }.limiterError: Emitted when registering or initializing a limiter fails. Passes{ name, error }.close: Emitted when the registry'sclose()method is called.
Error Handling
Learn how oss-ratelimit signals when a rate limit is exceeded and discover strategies for catching these errors and providing appropriate responses to your users.
Emphemeral Caching & Analyics RateLimit
Explore using ephemeral (in-memory) caching for high-performance, short-term rate limits and understand how oss-ratelimit helps track usage analytics.