Skip to content

Commit 81862a6

Browse files
committed
fixup: only set retryWrites=false for internal client
1 parent 78268cd commit 81862a6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

packages/mongodb-runner/src/mongoserver.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
isFailureToSetupListener,
1010
} from './mongologreader';
1111
import { Readable } from 'stream';
12-
import type { Document } from 'mongodb';
12+
import type { Document, MongoClientOptions } from 'mongodb';
1313
import { MongoClient } from 'mongodb';
1414
import path from 'path';
1515
import { once } from 'events';
@@ -342,12 +342,14 @@ export class MongoServer {
342342
mode: 'insert-new' | 'restore-check',
343343
): Promise<Error | null> {
344344
try {
345+
// directConnection + retryWrites let us write to `local` db on secondaries
346+
const clientOpts = { retryWrites: false };
345347
this.buildInfo = await this.withClient(async (client) => {
346348
// Insert the metadata entry, except if we're a freshly started mongos
347349
// (which does not have its own storage to persist)
348350
await this._ensureMatchingMetadataColl(client, mode);
349351
return await client.db('admin').command({ buildInfo: 1 });
350-
});
352+
}, clientOpts);
351353
} catch (err) {
352354
debug('failed to get buildInfo, treating as closed server', err);
353355
return err as Error;
@@ -362,11 +364,12 @@ export class MongoServer {
362364

363365
async withClient<Fn extends (client: MongoClient) => any>(
364366
fn: Fn,
367+
clientOptions: MongoClientOptions = {},
365368
): Promise<ReturnType<Fn>> {
366-
const client = await MongoClient.connect(
367-
// directConnection + retryWrites let us write to `local` db on secondaries
368-
`mongodb://${this.hostport}/?directConnection=true&retryWrites=false`,
369-
);
369+
const client = await MongoClient.connect(`mongodb://${this.hostport}/`, {
370+
directConnection: true,
371+
...clientOptions,
372+
});
370373
try {
371374
return await fn(client);
372375
} finally {

0 commit comments

Comments
 (0)