-
Notifications
You must be signed in to change notification settings - Fork 2
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
chore: bound the parallelism #162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* A minimal of p-limit that does not bring in new dependencies, and is not ESM. | ||
*/ | ||
|
||
type PromiseFactory<A> = () => Promise<A>; | ||
|
||
export function pLimit(concurrency: number): PLimit { | ||
const queue: Array<[PromiseFactory<any>, (x: any) => void, (reason?: any) => void]> = []; | ||
let activeCount = 0; | ||
let stopped = false; | ||
|
||
function dispatch() { | ||
if (activeCount < concurrency && queue.length > 0) { | ||
const [fac, resolve, reject] = queue.shift()!; | ||
activeCount++; | ||
fac().then( | ||
(r) => { | ||
// Start a new job before reporting back on the previous one | ||
resumeNext(); | ||
resolve(r); | ||
}, | ||
(e) => { | ||
// Start a new job before reporting back on the previous one | ||
resumeNext(); | ||
reject(e); | ||
} | ||
); | ||
} | ||
} | ||
|
||
function resumeNext() { | ||
activeCount--; | ||
if (stopped) { | ||
for (const [_, __, reject] of queue) { | ||
reject(new Error('Task has been cancelled')); | ||
} | ||
queue.splice(0, queue.length); | ||
} | ||
dispatch(); | ||
} | ||
|
||
const ret = <A>(promiseFactory: PromiseFactory<A>) => { | ||
return new Promise<A>((resolve, reject) => { | ||
queue.push([promiseFactory, resolve, reject]); | ||
dispatch(); | ||
}); | ||
}; | ||
Object.defineProperties(ret, { | ||
dispose: { | ||
value: () => { | ||
stopped = true; | ||
}, | ||
}, | ||
}); | ||
|
||
return ret as PLimit; | ||
} | ||
|
||
interface PLimit { | ||
dispose(): void; | ||
<A>(promiseFactory: PromiseFactory<A>): Promise<A>; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable @cdklabs/promiseall-no-unbounded-parallelism */ | ||
import { pLimit } from '../../lib/private/p-limit'; | ||
|
||
test('never running more than N jobs at once', async () => { | ||
const limit = pLimit(5); | ||
let current = 0; | ||
let max = 0; | ||
|
||
await Promise.all( | ||
Array.from({ length: 20 }).map(() => | ||
limit(async () => { | ||
max = Math.max(max, ++current); | ||
await sleep(1); | ||
--current; | ||
}) | ||
) | ||
); | ||
|
||
expect(max).toBeLessThanOrEqual(5); | ||
}); | ||
|
||
test('new jobs arent started after dispose is called', async () => { | ||
const limit = pLimit(2); | ||
let started = 0; | ||
|
||
await expect(() => | ||
Promise.all( | ||
Array.from({ length: 20 }).map(() => | ||
limit(async () => { | ||
started += 1; | ||
await sleep(0); | ||
throw new Error('oops'); | ||
}) | ||
) | ||
) | ||
).rejects.toThrow(/oops/); | ||
|
||
limit.dispose(); | ||
|
||
await sleep(20); | ||
|
||
// It may be that we started 1 more job here, but definitely not all 20 | ||
expect(started).toBeLessThanOrEqual(3); | ||
}); | ||
|
||
function sleep(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need/want to be this restrictive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Twenty is plenty