Skip to content

Commit

Permalink
consurent call fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Perki committed Mar 13, 2024
1 parent 14f4a17 commit 650eadc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ function PromisePoolRL(next, maxConcurent, options) {

function goNext(startWithDelay) {
if (done || rejected) {
console.log({countConcurentCalls, rejected});
if (countConcurentCalls === 0 && ! rejected) resolve();
return false;
}

// get next Promise to execute and return if false
// get next Promise to execute or goNext() if done to enventually resolve
const n = next();
if (! n) { done = true; return false}
if (! n) { done = true; return goNext(); }

countConcurentCalls++;
async function nn () { // async function to be able to delay

// --- rate limiting ---//
if (options.rateHz) {
const delay = (startWithDelay != null) ? startWithDelay : currentWait;
console.log({countConcurentCalls, delay, startWithDelay, currentWait});
await new Promise((r) => { setTimeout(r, delay) });
}
totalCount++;
Expand All @@ -71,7 +69,7 @@ function PromisePoolRL(next, maxConcurent, options) {
}

// start by filling up the pool
for (let i = 0; i <= maxConcurent; i++) {
for (let i = 0; i < maxConcurent; i++) {
if (! goNext(i * currentWait)) break;
}
});
Expand Down
3 changes: 1 addition & 2 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ test('Rate limiting', async (t) => {
let startedEntries = 0;
let doneEntries = 0;
const poolSize = 2;
const entries = 30;
const entries = 10;

const desiredRateHz = 1; // 1 per seconds

const startTime = Date.now();


function next() {
const id = ++count;
if (id === entries) return false;
Expand Down

0 comments on commit 650eadc

Please sign in to comment.