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

Fix AsyncResource propagation issue #71

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions async-hooks-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const AsyncResource = {
bind(fn, _type, thisArg) {
return fn.bind(thisArg);
},
};

export class AsyncLocalStorage {
getStore() {
return undefined;
}

run(_store, callback) {
return callback();
}
}
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Queue from 'yocto-queue';
import {AsyncResource} from '#async_hooks';

export default function pLimit(concurrency) {
if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
Expand Down Expand Up @@ -31,7 +32,9 @@ export default function pLimit(concurrency) {
};

const enqueue = (fn, resolve, args) => {
queue.enqueue(run.bind(undefined, fn, resolve, args));
queue.enqueue(
AsyncResource.bind(run.bind(undefined, fn, resolve, args)),
);

(async () => {
// This function needs to wait until the next microtask before comparing
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
},
"type": "module",
"exports": "./index.js",
"imports": {
"#async_hooks": {
"node": "async_hooks",
"default": "./async-hooks-stub.js"
}
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
Expand All @@ -20,7 +26,8 @@
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"async-hooks-stub.js"
],
"keywords": [
"promise",
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import inRange from 'in-range';
import timeSpan from 'time-span';
import randomInt from 'random-int';
import {AsyncLocalStorage} from '#async_hooks';
import pLimit from './index.js';

Check failure on line 7 in test.js

View workflow job for this annotation

GitHub Actions / Node.js 16

`./index.js` import should occur before import of `#async_hooks`

test('concurrency: 1', async t => {
const input = [
Expand Down Expand Up @@ -40,6 +41,23 @@
await Promise.all(input);
});

test('propagates async execution context properly', async t => {
const concurrency = 2;
const limit = pLimit(concurrency);
const store = new AsyncLocalStorage();

const checkId = async id => {
await Promise.resolve();
t.is(id, store.getStore()?.id);
};

const startContext = async id => store.run({id}, () => limit(checkId, id));

await Promise.all(
Array.from({length: 100}, (_, id) => startContext(id)),
);
});

test('non-promise returning function', async t => {
await t.notThrowsAsync(async () => {
const limit = pLimit(1);
Expand Down
Loading