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

feat(NODE-5639): keep executing context on queued commands #3871

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cmap/connection_pool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AsyncResource } from 'async_hooks';
import { clearTimeout, setTimeout } from 'timers';

import type { ObjectId } from '../bson';
Expand Down Expand Up @@ -356,7 +357,9 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
new ConnectionCheckOutStartedEvent(this)
);

const waitQueueMember: WaitQueueMember = { callback };
const callbackWithContext = AsyncResource.bind(callback);

const waitQueueMember: WaitQueueMember = { callback: callbackWithContext };
const waitQueueTimeoutMS = this.options.waitQueueTimeoutMS;
if (waitQueueTimeoutMS) {
waitQueueMember.timer = setTimeout(() => {
Expand Down
29 changes: 29 additions & 0 deletions test/unit/cmap/connection_pool.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { AsyncLocalStorage } = require('async_hooks');
const { ConnectionPool } = require('../../mongodb');
const { WaitQueueTimeoutError } = require('../../mongodb');
const mock = require('../../tools/mongodb-mock/index');
Expand Down Expand Up @@ -148,6 +149,34 @@ describe('Connection Pool', function () {
});
});

it('should propagate context on checkout callbacks', function (done) {
mockMongod.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(mock.HELLO);
}
});

const storage = new AsyncLocalStorage();
const context = { id: Math.random() };
const pool = new ConnectionPool(stubServer, {
maxPoolSize: 1,
waitQueueTimeoutMS: 200,
hostAddress: mockMongod.hostAddress()
});

pool.ready();
storage.enterWith(context);

pool.checkOut((err, conn) => {
expect(err).to.not.exist;
expect(conn).to.exist;
expect(storage.getStore()).to.eq(context);
storage.exit();
done();
});
});

describe('minPoolSize population', function () {
let clock, timerSandbox;
beforeEach(() => {
Expand Down