Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(txns): support mongos pinning in unified topology
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 27, 2019
1 parent f89eb61 commit 78dab5d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/sdam/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Topology extends EventEmitter {
translateReadPreference(options);
const readPreference = options.readPreference || ReadPreference.primary;

this.selectServer(readPreferenceServerSelector(readPreference), (err, server) => {
this.selectServer(readPreferenceServerSelector(readPreference), options, (err, server) => {
if (err) {
if (typeof callback === 'function') {
callback(err, null);
Expand Down Expand Up @@ -310,14 +310,29 @@ class Topology extends EventEmitter {
options
);

const isSharded = this.description.type === TopologyType.Sharded;
const session = options.session;
const transaction = session && session.transaction;

if (isSharded && transaction && transaction.server) {
callback(null, transaction.server);
return;
}

selectServers(
this,
selector,
options.serverSelectionTimeoutMS,
process.hrtime(),
(err, servers) => {
if (err) return callback(err, null);
callback(null, randomSelection(servers));

const selectedServer = randomSelection(servers);
if (isSharded && transaction && transaction.isActive) {
transaction.pinServer(selectedServer);
}

callback(null, selectedServer);
}
);
}
Expand Down Expand Up @@ -505,7 +520,7 @@ class Topology extends EventEmitter {
translateReadPreference(options);
const readPreference = options.readPreference || ReadPreference.primary;

this.selectServer(readPreferenceServerSelector(readPreference), (err, server) => {
this.selectServer(readPreferenceServerSelector(readPreference), options, (err, server) => {
if (err) {
callback(err, null);
return;
Expand Down Expand Up @@ -868,7 +883,7 @@ function executeWriteOperation(args, options, callback) {
isRetryableWritesSupported(topology) &&
!options.session.inTransaction();

topology.selectServer(writableServerSelector(), (err, server) => {
topology.selectServer(writableServerSelector(), options, (err, server) => {
if (err) {
callback(err, null);
return;
Expand Down

0 comments on commit 78dab5d

Please sign in to comment.