Skip to content

feat(NODE-5614): Add support for explicit resource management #4177

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

Merged
merged 18 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/beta.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './index';
import * as contents from './index';
import { configureExplicitResourceManagement } from './resource_management';

export = { ...contents, configureExplicitResourceManagement };
9 changes: 7 additions & 2 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,11 @@ export class ChangeStream<
}

Symbol.asyncDispose &&
(ChangeStream.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(ChangeStream.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});
9 changes: 7 additions & 2 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,11 @@ class ReadableCursorStream extends Readable {
}

Symbol.asyncDispose &&
(AbstractCursor.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(AbstractCursor.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});
9 changes: 7 additions & 2 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
}

Symbol.asyncDispose &&
(MongoClient.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(MongoClient.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});

/**
Expand Down
42 changes: 31 additions & 11 deletions src/resource_management.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AbstractCursor, ChangeStream } from './beta';
import { ChangeStream } from './change_stream';
import { AbstractCursor } from './cursor/abstract_cursor';
import { MongoClient } from './mongo_client';
import { ClientSession } from './sessions';

Expand All @@ -12,7 +13,6 @@ export interface AsyncDisposable {
}

/**
* @public
* @beta
*
* Attaches `Symbol.asyncDispose` methods to the MongoClient, Cursors, sessions and change streams
Expand All @@ -26,7 +26,7 @@ export interface AsyncDisposable {
* Example:
*
* ```typescript
* import { load, MongoClient } from 'mongodb/beta';
* import { configureExplicitResourceManagement, MongoClient } from 'mongodb/lib/beta';
*
* Symbol.asyncDispose ??= Symbol('dispose');
* load();
Expand All @@ -36,22 +36,42 @@ export interface AsyncDisposable {
*/
export function configureExplicitResourceManagement() {
Symbol.asyncDispose &&
(MongoClient.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(MongoClient.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});

Symbol.asyncDispose &&
(ClientSession.prototype[Symbol.asyncDispose] = async function () {
await this.endSession({ force: true });
Object.defineProperty(AbstractCursor.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});

Symbol.asyncDispose &&
(AbstractCursor.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(ChangeStream.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { close(): Promise<void> }) {
await this.close();
},
enumerable: false,
configurable: true,
writable: true
});

Symbol.asyncDispose &&
(ChangeStream.prototype[Symbol.asyncDispose] = async function () {
await this.close();
Object.defineProperty(ClientSession.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { endSession(): Promise<void> }) {
await this.endSession();
},
enumerable: false,
configurable: true,
writable: true
});
}
9 changes: 7 additions & 2 deletions src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,13 @@ export class ClientSession
}

Symbol.asyncDispose &&
(ClientSession.prototype[Symbol.asyncDispose] = async function () {
await this.endSession({ force: true });
Object.defineProperty(ClientSession.prototype, Symbol.asyncDispose, {
value: async function asyncDispose(this: { endSession(): Promise<void> }) {
await this.endSession();
},
enumerable: false,
configurable: true,
writable: true
});

const MAX_WITH_TRANSACTION_TIMEOUT = 120000;
Expand Down
3 changes: 0 additions & 3 deletions test/explicit-resource-management/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import { setTimeout } from 'timers/promises';

// @ts-expect-error Assigning readonly property.
Symbol.asyncDispose ??= Symbol('dispose');

async function setUpCollection(client: MongoClient) {
const collection = client.db('foo').collection<{ name: string }>('bar');
const documents: Array<{ name: string }> = Array.from({ length: 5 }).map(i => ({
Expand Down