PRININIT / src /queue.ts
Rachit-Tw's picture
Upload 50 files
7d9309d verified
Raw
History Blame Contribute Delete
596 Bytes
import pLimit from 'p-limit'
const MAX_CONCURRENT = 10
const MAX_QUEUE_DEPTH = 50
const queueLimit = pLimit(MAX_CONCURRENT)
let queueLength = 0
export function getQueueLength(): number {
return queueLength
}
export async function enqueueTask<T>(
fn: () => Promise<T>
): Promise<{queued: boolean; result?: T; error?: string}> {
if (queueLength >= MAX_QUEUE_DEPTH) {
return {queued: false, error: 'too_many_requests'}
}
queueLength++
try {
const result = await queueLimit(fn)
return {queued: true, result}
} finally {
queueLength--
}
}