-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the linter rule that checks for bounded parallelism, and mark the 2 locations that do `Promise.all` as bounded.
- Loading branch information
Showing
11 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.