Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit concurrency #344

Merged
merged 4 commits into from
Oct 4, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/BaseDownloadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const yaml = require('js-yaml');
const fs = require('fs-extra');
const hash = require('object-hash');
const clone = require('clone');
const pLimit = require('p-limit');

const CompositeController = require('./CompositeController');

Expand All @@ -28,6 +29,11 @@ module.exports = class BaseDownloadController extends CompositeController {
constructor(params) {
params.finalizerString = params.finalizerString || 'children.downloads.deploy.razee.io';
super(params);
this._limit = pLimit(5);
}

get limit() {
return this._limit;
}

async added() {
Expand Down Expand Up @@ -156,21 +162,25 @@ module.exports = class BaseDownloadController extends CompositeController {
if (Array.isArray(file)) {
let error;
let res = await Promise.all(file.map(async f => {
try {
return await this._decomposeFile(f);
} catch (e) {
error = error || e;
}
return this.limit(async () => {
try {
return await this._decomposeFile(f);
} catch (e) {
error = error || e;
}
});
}));
return error ? Promise.reject(error) : res;
} else if (kind.toLowerCase() == 'list' && Array.isArray(items)) {
let error;
let res = await Promise.all(items.map(async f => {
try {
return await this._decomposeFile(f);
} catch (e) {
error = error || e;
}
return this.limit(async () => {
try {
return await this._decomposeFile(f);
} catch (e) {
error = error || e;
}
});
}));
return error ? Promise.reject(error) : res;
} else if (file) {
Expand Down