The function type being rate limited
Function to apply rate limiting to
Rate limiting configuration options or bucket options
Rate-limited version of the original function (async)
// Throwing rate limiter with options
const rateLimitedFn = rateLimit(fn, {
maxCalls: 10,
windowMs: 1000,
throws: true,
onLimitReached: (error) => {
console.error('Rate limit exceeded:', error.message);
}
});
for (let i = 0; i < 10; i++) {
await rateLimitedFn();
}
// will throw an error
await rateLimitedFn();
// With an existing bucket (useful for persistence)
const bucket = new RateLimitTokenBucket({
capacity: 10,
refillIntervalMs: 100,
save: async (state) => redis.set('key', JSON.stringify(state)),
load: async () => JSON.parse(await redis.get('key'))
});
const rateLimitedFn = rateLimit(fn, {
bucket,
throws: true
});
// When bucket.isSaveable is true, load() is called before each check
// and save() is called after each successful consume
await rateLimitedFn();
Rate limiter that restricts function calls to a specified number per time window.
Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.
The function type being rate limited
Function to apply rate limiting to
Rate limiting configuration options or bucket options
Rate-limited version of the original function (async)
// Throwing rate limiter with options
const rateLimitedFn = rateLimit(fn, {
maxCalls: 10,
windowMs: 1000,
throws: true,
onLimitReached: (error) => {
console.error('Rate limit exceeded:', error.message);
}
});
for (let i = 0; i < 10; i++) {
await rateLimitedFn();
}
// will throw an error
await rateLimitedFn();
// With an existing bucket (useful for persistence)
const bucket = new RateLimitTokenBucket({
capacity: 10,
refillIntervalMs: 100,
save: async (state) => redis.set('key', JSON.stringify(state)),
load: async () => JSON.parse(await redis.get('key'))
});
const rateLimitedFn = rateLimit(fn, {
bucket,
throws: true
});
// When bucket.isSaveable is true, load() is called before each check
// and save() is called after each successful consume
await rateLimitedFn();
Rate limiter that restricts function calls to a specified number per time window.
Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.
The function type being rate limited
Function to apply rate limiting to
Rate limiting configuration options or bucket options
Rate-limited version of the original function (async)
// Throwing rate limiter with options
const rateLimitedFn = rateLimit(fn, {
maxCalls: 10,
windowMs: 1000,
throws: true,
onLimitReached: (error) => {
console.error('Rate limit exceeded:', error.message);
}
});
for (let i = 0; i < 10; i++) {
await rateLimitedFn();
}
// will throw an error
await rateLimitedFn();
// With an existing bucket (useful for persistence)
const bucket = new RateLimitTokenBucket({
capacity: 10,
refillIntervalMs: 100,
save: async (state) => redis.set('key', JSON.stringify(state)),
load: async () => JSON.parse(await redis.get('key'))
});
const rateLimitedFn = rateLimit(fn, {
bucket,
throws: true
});
// When bucket.isSaveable is true, load() is called before each check
// and save() is called after each successful consume
await rateLimitedFn();
Rate limiter that restricts function calls to a specified number per time window.
Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.
The function type being rate limited
Function to apply rate limiting to
Rate limiting configuration options or bucket options
Rate-limited version of the original function (async)
// Throwing rate limiter with options
const rateLimitedFn = rateLimit(fn, {
maxCalls: 10,
windowMs: 1000,
throws: true,
onLimitReached: (error) => {
console.error('Rate limit exceeded:', error.message);
}
});
for (let i = 0; i < 10; i++) {
await rateLimitedFn();
}
// will throw an error
await rateLimitedFn();
// With an existing bucket (useful for persistence)
const bucket = new RateLimitTokenBucket({
capacity: 10,
refillIntervalMs: 100,
save: async (state) => redis.set('key', JSON.stringify(state)),
load: async () => JSON.parse(await redis.get('key'))
});
const rateLimitedFn = rateLimit(fn, {
bucket,
throws: true
});
// When bucket.isSaveable is true, load() is called before each check
// and save() is called after each successful consume
await rateLimitedFn();
Rate limiter that restricts function calls to a specified number per time window.
Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.