remove dev comments
This commit is contained in:
@@ -1,21 +1,11 @@
|
||||
// src/utils/rateLimitedQueue.ts
|
||||
// ------------------------------------------------------------
|
||||
// Enhanced FIFO queue with status tracking for visual feedback
|
||||
// ------------------------------------------------------------
|
||||
|
||||
import dotenv from "dotenv";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
/**
|
||||
* Delay (in **milliseconds**) between two consecutive API calls.
|
||||
* Defaults to **2000 ms** (2 seconds) when not set or invalid.
|
||||
*/
|
||||
const RATE_LIMIT_DELAY_MS = Number.parseInt(process.env.AI_RATE_LIMIT_DELAY_MS ?? "2000", 10) || 2000;
|
||||
|
||||
/**
|
||||
* Internal task type with ID tracking for status updates
|
||||
*/
|
||||
export type Task<T = unknown> = () => Promise<T>;
|
||||
|
||||
interface QueuedTask {
|
||||
@@ -28,7 +18,7 @@ export interface QueueStatus {
|
||||
queueLength: number;
|
||||
isProcessing: boolean;
|
||||
estimatedWaitTime: number; // in milliseconds
|
||||
currentPosition?: number; // position of specific request
|
||||
currentPosition?: number;
|
||||
}
|
||||
|
||||
class RateLimitedQueue {
|
||||
@@ -37,10 +27,6 @@ class RateLimitedQueue {
|
||||
private delayMs = RATE_LIMIT_DELAY_MS;
|
||||
private lastProcessedAt = 0;
|
||||
|
||||
/**
|
||||
* Schedule a task with ID tracking. Returns a Promise that resolves/rejects
|
||||
* with the task result once the queue reaches it.
|
||||
*/
|
||||
add<T>(task: Task<T>, taskId?: string): Promise<T> {
|
||||
const id = taskId || this.generateTaskId();
|
||||
|
||||
@@ -61,23 +47,17 @@ class RateLimitedQueue {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current queue status for visual feedback
|
||||
*/
|
||||
getStatus(taskId?: string): QueueStatus {
|
||||
const queueLength = this.queue.length;
|
||||
const now = Date.now();
|
||||
|
||||
// Calculate estimated wait time
|
||||
let estimatedWaitTime = 0;
|
||||
if (queueLength > 0) {
|
||||
if (this.processing) {
|
||||
// Time since last request + remaining delay + queue length * delay
|
||||
const timeSinceLastRequest = now - this.lastProcessedAt;
|
||||
const remainingDelay = Math.max(0, this.delayMs - timeSinceLastRequest);
|
||||
estimatedWaitTime = remainingDelay + (queueLength - 1) * this.delayMs;
|
||||
} else {
|
||||
// Queue will start immediately, so just queue length * delay
|
||||
estimatedWaitTime = queueLength * this.delayMs;
|
||||
}
|
||||
}
|
||||
@@ -88,35 +68,25 @@ class RateLimitedQueue {
|
||||
estimatedWaitTime
|
||||
};
|
||||
|
||||
// Find position of specific task if ID provided
|
||||
if (taskId) {
|
||||
const position = this.queue.findIndex(item => item.id === taskId);
|
||||
if (position >= 0) {
|
||||
status.currentPosition = position + 1; // 1-based indexing for user display
|
||||
status.currentPosition = position + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the delay at runtime
|
||||
*/
|
||||
setDelay(ms: number): void {
|
||||
if (!Number.isFinite(ms) || ms < 0) return;
|
||||
this.delayMs = ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current delay setting
|
||||
*/
|
||||
getDelay(): number {
|
||||
return this.delayMs;
|
||||
}
|
||||
|
||||
// ---------------------------------------
|
||||
// Internal helpers
|
||||
// ---------------------------------------
|
||||
private async process(): Promise<void> {
|
||||
if (this.processing) return;
|
||||
this.processing = true;
|
||||
@@ -128,7 +98,6 @@ class RateLimitedQueue {
|
||||
this.lastProcessedAt = Date.now();
|
||||
await next.task();
|
||||
|
||||
// Wait before the next one (only if there are more tasks)
|
||||
if (this.queue.length > 0) {
|
||||
await new Promise((r) => setTimeout(r, this.delayMs));
|
||||
}
|
||||
@@ -142,21 +111,12 @@ class RateLimitedQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Export singleton instance and convenience functions
|
||||
// ------------------------------------------------------------
|
||||
const queue = new RateLimitedQueue();
|
||||
|
||||
/**
|
||||
* Helper for convenience: `enqueueApiCall(() => fetch(...), 'optional-id')`.
|
||||
*/
|
||||
export function enqueueApiCall<T>(task: Task<T>, taskId?: string): Promise<T> {
|
||||
return queue.add(task, taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current queue status for visual feedback
|
||||
*/
|
||||
export function getQueueStatus(taskId?: string): QueueStatus {
|
||||
return queue.getStatus(taskId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user