mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 10:27:09 +00:00
26 lines
499 B
JavaScript
26 lines
499 B
JavaScript
class ConcurrencyLimiter {
|
|
|
|
constructor(maxConcurrent) {
|
|
this.maxConcurrent = maxConcurrent;
|
|
this.active = 0;
|
|
this.queue = [];
|
|
}
|
|
|
|
async enqueue(task) {
|
|
if (this.active >= this.maxConcurrent) {
|
|
await new Promise(resolve => this.queue.push(resolve));
|
|
}
|
|
this.active++;
|
|
try {
|
|
return await task();
|
|
} finally {
|
|
this.active--;
|
|
if (this.queue.length > 0) {
|
|
this.queue.shift()();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ConcurrencyLimiter;
|