Use Refresh and Increase Reservoir Intervals
masterReservoir Intervals allow for bursty execution by automatically managing the reservoir value (the number of jobs allowed to run).
Refresh Interval
Resets the reservoir to a specific amount at a fixed interval. Useful for 'X requests per Y seconds' patterns.
const limiter = new Bottleneck({
reservoir: 100, // initial value
reservoirRefreshAmount: 100,
reservoirRefreshInterval: 60 * 1000, // must be divisible by 250
maxConcurrent: 1,
minTime: 333
});Increase Interval
Gradually increments the reservoir by a specific amount at a fixed interval. Useful for APIs with sliding window limits (like Shopify).
const limiter = new Bottleneck({
reservoir: 40, // initial value
reservoirIncreaseAmount: 2,
reservoirIncreaseInterval: 1000, // must be divisible by 250
reservoirIncreaseMaximum: 40,
maxConcurrent: 5,
minTime: 250
});Important Warnings:
- Always use
minTimeandmaxConcurrentalongside Reservoir Intervals. Without them, a reservoir refresh might trigger a massive burst of jobs all at once. - Intervals start at creation. If jobs are added right before a refresh, they might execute immediately after the refresh, effectively doubling the burst.
- Memory Management: Reservoir intervals prevent garbage collection. Call
limiter.disconnect()to clear intervals and free memory.
// Refresh Interval Example
const limiter = new Bottleneck({
reservoir: 100,
reservoirRefreshAmount: 100,
reservoirRefreshInterval: 60 * 1000,
maxConcurrent: 1,
minTime: 333
});
// Increase Interval Example
const limiter = new Bottleneck({
reservoir: 40,
reservoirIncreaseAmount: 2,
reservoirIncreaseInterval: 1000,
reservoirIncreaseMaximum: 40,
maxConcurrent: 5,
minTime: 250
});