Skip to content

Commit

Permalink
fix(execute-operation): return promise on session support check
Browse files Browse the repository at this point in the history
3.3.0 introduced an extra roundtrip of selection in order to
accurately determine if sessions were supported by the topology.
Unfortunately, the fix only supported callbacks. So, if a server
wasn't actually available immediately, we would return `undefined`
in cases when promises were requested for operation execution.

NODE-2136
  • Loading branch information
mbroadst committed Aug 28, 2019
1 parent b3ae4c5 commit a976c14
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions lib/operations/execute_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ function executeOperation(topology, operation, callback) {
!operation.hasAspect(Aspect.SKIP_SESSION) &&
topology.shouldCheckForSessionSupport()
) {
// TODO: this is only supported for unified topology, the first part of this check
// should go away when we drop legacy topology types.
topology.selectServer(ReadPreference.primaryPreferred, err => {
if (err) {
callback(err);
return;
}

executeOperation(topology, operation, callback);
});

return;
return selectServerForSessionSupport(topology, operation, callback);
}

const Promise = topology.s.promiseLibrary;
Expand Down Expand Up @@ -179,4 +168,31 @@ function executeWithServerSelection(topology, operation, callback) {
});
}

// TODO: This is only supported for unified topology, it should go away once
// we remove support for legacy topology types.
function selectServerForSessionSupport(topology, operation, callback) {
const Promise = topology.s.promiseLibrary;

let result;
if (typeof callback !== 'function') {
result = new Promise((resolve, reject) => {
callback = (err, result) => {
if (err) return reject(err);
resolve(result);
};
});
}

topology.selectServer(ReadPreference.primaryPreferred, err => {
if (err) {
callback(err);
return;
}

executeOperation(topology, operation, callback);
});

return result;
}

module.exports = executeOperation;

0 comments on commit a976c14

Please sign in to comment.