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

Large number of parallel http requests for larger lock files #3

Open
sven-wermers opened this issue Dec 2, 2022 · 0 comments
Open

Comments

@sven-wermers
Copy link

Hello,
It seems that the await Promise.all(..) (referenced code below) causes that all packages were treated in parallel. Therefore all http request to the registry happening more or less parallel. Therefore, all http requests to the registry are made more or less in parallel. This causes problems when the number of dependencies reaches a certain limit.

export async function traitNpmLockFile(
lockFile: string,
opts: TraitOptions,
): Promise<void> {
const lockFileObject = await parseNpmLockFile(lockFile);
try {
await Promise.all(
Object.keys(lockFileObject.packages ?? {})
.filter((pkg) => !!pkg)
.map(async (pkg) => {
await traitPackage(

To solve this issues a function could help which only excutes certain amount task in parallel like the following one:

async function parallel<T>(arr: T[], fct: (x: T) => Promise<void>, workerCnt = 10) {
  const tmp = [...arr];
  const worker = async () => {
    while (tmp.length > 0) {
      await fct(tmp.pop());
    }
  };
  const workers = [];
  for (let i = 0; i < workerCnt; i++) {
    workers.push(worker());
  }
  await Promise.all(workers);
}

The effected code segment could then look like this:

export async function traitNpmLockFile(
  lockFile: string,
  opts: TraitOptions,
): Promise<void> {
  const lockFileObject = await parseNpmLockFile(lockFile);

  try {
    const allPkg = Object.keys(lockFileObject.packages ?? {}).filter((pkg) => !!pkg);
    await parallel(allPkg, async (pkg) => {
      await traitPackage(
        lockFileObject.packages,
        pkg,
        pkg.replace(/^.*node_modules\//g, ''),
        lockFileObject.packages[pkg].from ??
          lockFileObject.packages[pkg].version,
        {
          ...opts,
          lockFile,
        },
      );
    });

Best regards
Sven

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant