Created
January 10, 2025 09:27
-
-
Save hckhanh/aa5147f3931d68313b80193d9103b67d to your computer and use it in GitHub Desktop.
How to upload with limit concurrency connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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