From 73300310b71146238d6996a31d102c1537c57f2e Mon Sep 17 00:00:00 2001 From: rodrigorodriguez Date: Mon, 10 Oct 2022 00:08:29 -0300 Subject: [PATCH] new(all): Paralel bot loading. --- packages/core.gbapp/services/GBMinService.ts | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 2dab8ed2..849c462b 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -177,6 +177,27 @@ export class GBMinService { let i = 0; bar1.start(instances.length, i, { botId: "Boot" }); + const throttledPromiseAll = async (promises) => { + const MAX_IN_PROCESS = 15; + const results = new Array(promises.length); + + async function doBlock(startIndex) { + // Shallow-copy a block of promises to work on + const currBlock = promises.slice(startIndex, startIndex + MAX_IN_PROCESS); + // Await the completion. If any fail, it will throw and that's good. + const blockResults = await Promise.all(currBlock); + // Assuming all succeeded, copy the results into the results array + for (let ix = 0; ix < blockResults.length; ix++) { + results[ix + startIndex] = blockResults[ix]; + } + } + + for (let iBlock = 0; iBlock < promises.length; iBlock += MAX_IN_PROCESS) { + await doBlock(iBlock); + } + return results; + }; + const p = (async (instance) => { try { bar1.update(i, { botId: instance.botId }); @@ -189,8 +210,8 @@ export class GBMinService { GBLog.error(`Error mounting bot ${instance.botId}: ${error.message}\n${error.stack}`); } }).bind(this); - - await Promise.all(instances.map(instance => p(instance))); + + await throttledPromiseAll(instances.map(instance => p(instance))); bar1.stop();