Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Created January 10, 2025 09:27
Show Gist options
  • Save hckhanh/aa5147f3931d68313b80193d9103b67d to your computer and use it in GitHub Desktop.
Save hckhanh/aa5147f3931d68313b80193d9103b67d to your computer and use it in GitHub Desktop.
How to upload with limit concurrency connections
/**
* Uploads artifacts with a set concurrency limit.
*/
async function uploadWithConcurrency(
artifacts: BunnyArtifact[],
concurrency: number,
task: (artifact: BunnyArtifact) => Promise<void>
): Promise<void> {
const queue: Promise<void>[] = []
for (const artifact of artifacts) {
const taskPromise = task(artifact)
queue.push(taskPromise)
// Await if queue exceeds concurrency
if (queue.length >= concurrency) {
await Promise.race(queue)
// Remove the completed items
queue.splice(
queue.findIndex((p) => p === taskPromise),
1
)
}
}
await Promise.all(queue) // Wait for remaining uploads
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment