JAVASCRIPT
Proactive API Request Throttling using a Queue
Implement a JavaScript queue to proactively throttle outgoing API requests, preventing rate limit errors by spacing out calls to external services.
/**
* A simple queue-based throttler for API requests.
* Prevents hitting API rate limits by spacing out requests.
*/
class ApiThrottler {
constructor(requestsPerInterval, intervalMs) {
this.requestsPerInterval = requestsPerInterval;
this.intervalMs = intervalMs;
this.queue = [];
this.activeRequests = 0;
this.lastIntervalStartTime = 0;
this.requestsMadeInCurrentInterval = 0;
this.timer = null;
console.log(`Throttler initialized: ${requestsPerInterval} requests/${intervalMs}ms`);
}
/**
* Adds a request to the queue and processes it.
* @param {Function} requestFn - An async function that performs the API call.
* @returns {Promise<any>} A promise that resolves with the API call's result.
*/
addRequest(requestFn) {
return new Promise((resolve, reject) => {
this.queue.push({ requestFn, resolve, reject });
this.processQueue();
});
}
processQueue() {
if (this.queue.length === 0 || this.activeRequests >= this.requestsPerInterval) {
// No requests in queue or max active requests reached for this interval
return;
}
const now = Date.now();
if (now - this.lastIntervalStartTime > this.intervalMs) {
// New interval started
this.lastIntervalStartTime = now;
this.requestsMadeInCurrentInterval = 0;
}
if (this.requestsMadeInCurrentInterval >= this.requestsPerInterval) {
// Rate limit reached for current interval, wait for next interval
if (!this.timer) {
const timeToWait = this.intervalMs - (now - this.lastIntervalStartTime);
console.log(`Rate limit hit, waiting ${timeToWait}ms before next interval.`);
this.timer = setTimeout(() => {
this.timer = null;
this.lastIntervalStartTime = Date.now(); // Reset for new interval
this.requestsMadeInCurrentInterval = 0;
this.processQueue(); // Try processing again
}, timeToWait);
}
return;
}
const { requestFn, resolve, reject } = this.queue.shift();
this.activeRequests++;
this.requestsMadeInCurrentInterval++;
console.log(`Processing request. Active: ${this.activeRequests}, In interval: ${this.requestsMadeInCurrentInterval}/${this.requestsPerInterval}`);
requestFn()
.then(result => {
resolve(result);
})
.catch(error => {
reject(error);
})
.finally(() => {
this.activeRequests--;
this.processQueue(); // Process next item
});
// Ensure processing continues for subsequent items if possible within the current interval
// Set a micro-timeout to avoid blocking the event loop for subsequent synchronous calls.
// This is important if requestFn resolves immediately or very quickly.
// setTimeout(() => this.processQueue(), 0); // Not strictly needed with async requestFn but good practice
}
}
// --- Example Usage ---
// Simulate an API call function
async function simulateApiCall(id) {
const delay = Math.random() * 500 + 100; // Simulate network delay
await new Promise(resolve => setTimeout(delay, resolve));
if (Math.random() < 0.1) { // 10% chance of error
throw new Error(`API Error for ID ${id}`);
}
return `Data for ID ${id} (processed in ${delay.toFixed(0)}ms)`;
}
// Create a throttler: 3 requests per 1000ms (1 second)
const apiThrottler = new ApiThrottler(3, 1000);
async function makeThrottledCalls() {
const promises = [];
console.log('
--- Adding 10 requests to the throttler ---');
for (let i = 1; i <= 10; i++) {
promises.push(apiThrottler.addRequest(() => simulateApiCall(i))
.then(result => console.log(`SUCCESS: ${result}`))
.catch(error => console.error(`ERROR: ${error.message}`))
);
}
await Promise.allSettled(promises);
console.log('
--- All throttled requests finished ---');
}
makeThrottledCalls();
How it works: This JavaScript snippet provides a class, `ApiThrottler`, designed to proactively manage the rate of outgoing API requests. Instead of reactively handling 429 "Too Many Requests" errors, this throttler uses a queue to space out requests. It accepts a `requestsPerInterval` and `intervalMs` (e.g., 3 requests per 1000ms). When `addRequest` is called, the request is added to an internal queue. The `processQueue` method then dispatches requests only when the defined rate limits are not exceeded within the current time interval. If the limit is reached, it pauses and waits until the next interval before resuming, ensuring calls to external APIs remain within acceptable rate limits. This strategy helps prevent reaching the server's rate limits and ensures a smoother integration experience.