✨ Restrict # concurrent downloads
This commit is contained in:
parent
9e8f1e11d8
commit
eb11b6414a
2 changed files with 8 additions and 5 deletions
|
@ -62,7 +62,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
|
|||
//start download, making sure that .zip file downloads first
|
||||
const indexOfLastFile = solidpkg.files.length - 1;
|
||||
const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile]));
|
||||
const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper({ ...createUrlObject(file), useCurl: true }));
|
||||
const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper.bind(null, { ...createUrlObject(file), useCurl: true }));
|
||||
|
||||
//we can parse the file entries as soon as the .zip file is downloaded
|
||||
const fileEntries = readSsnFile(await zipFile);
|
||||
|
@ -72,7 +72,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
|
|||
|
||||
//Then we need to wait for disks to finish download before we can extract individual files
|
||||
//TODO: we can optimize this to already extract some files as soon as their relevant parts are downloaded
|
||||
const diskFilenames = await Promise.all(diskFiles);
|
||||
const diskFilenames: string[] = await taskManager(diskFiles, 3); //max. of 3 concurrent downloads
|
||||
|
||||
//TODO: Verify that downloaded files match the hash in `solidpkg.pieces`
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
export default function taskManager(tasks: Array<() => Promise<void>>, maxConcurrentTasks: number): Promise<void> {
|
||||
export default function taskManager(tasks: Array<() => Promise<void>>, maxConcurrentTasks: number): Promise<any[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const returnValues = Array(tasks.length);
|
||||
const remainingTasks = tasks;
|
||||
let currentlyRunningTasks = 0;
|
||||
|
||||
|
@ -7,13 +8,15 @@ export default function taskManager(tasks: Array<() => Promise<void>>, maxConcur
|
|||
//Exit if we completed all tasks
|
||||
if (remainingTasks.length === 0) {
|
||||
if (currentlyRunningTasks === 0) {
|
||||
return resolve();
|
||||
return resolve(returnValues);
|
||||
}
|
||||
} else {
|
||||
//If there is at least one task left, complete it
|
||||
const curTask = remainingTasks.pop() as () => Promise<void>;
|
||||
const curTaskIndex = remainingTasks.length;
|
||||
currentlyRunningTasks += 1;
|
||||
curTask().then(() => {
|
||||
curTask().then((...result) => {
|
||||
returnValues[curTaskIndex] = result;
|
||||
currentlyRunningTasks -= 1;
|
||||
return startNewTask();
|
||||
}).catch((error) => {
|
||||
|
|
Loading…
Reference in a new issue