Restrict # concurrent downloads

This commit is contained in:
C-3PO 2018-10-18 01:57:18 +02:00
parent 9e8f1e11d8
commit eb11b6414a
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 8 additions and 5 deletions

View file

@ -62,7 +62,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar
//start download, making sure that .zip file downloads first //start download, making sure that .zip file downloads first
const indexOfLastFile = solidpkg.files.length - 1; const indexOfLastFile = solidpkg.files.length - 1;
const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile])); 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 //we can parse the file entries as soon as the .zip file is downloaded
const fileEntries = readSsnFile(await zipFile); 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 //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 //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` //TODO: Verify that downloaded files match the hash in `solidpkg.pieces`

View file

@ -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) => { return new Promise((resolve, reject) => {
const returnValues = Array(tasks.length);
const remainingTasks = tasks; const remainingTasks = tasks;
let currentlyRunningTasks = 0; let currentlyRunningTasks = 0;
@ -7,13 +8,15 @@ export default function taskManager(tasks: Array<() => Promise<void>>, maxConcur
//Exit if we completed all tasks //Exit if we completed all tasks
if (remainingTasks.length === 0) { if (remainingTasks.length === 0) {
if (currentlyRunningTasks === 0) { if (currentlyRunningTasks === 0) {
return resolve(); return resolve(returnValues);
} }
} else { } else {
//If there is at least one task left, complete it //If there is at least one task left, complete it
const curTask = remainingTasks.pop() as () => Promise<void>; const curTask = remainingTasks.pop() as () => Promise<void>;
const curTaskIndex = remainingTasks.length;
currentlyRunningTasks += 1; currentlyRunningTasks += 1;
curTask().then(() => { curTask().then((...result) => {
returnValues[curTaskIndex] = result;
currentlyRunningTasks -= 1; currentlyRunningTasks -= 1;
return startNewTask(); return startNewTask();
}).catch((error) => { }).catch((error) => {