@@ -464,7 +464,14 @@ function connectProxies(self, servers) {
464464 }
465465}
466466
467- function pickProxy ( self ) {
467+ function pickProxy ( self , session ) {
468+ // TODO: Destructure :)
469+ const transaction = session && session . transaction ;
470+
471+ if ( transaction && transaction . server ) {
472+ return transaction . server ;
473+ }
474+
468475 // Get the currently connected Proxies
469476 var connectedProxies = self . connectedProxies . slice ( 0 ) ;
470477
@@ -488,15 +495,22 @@ function pickProxy(self) {
488495 }
489496 } ) ;
490497
498+ let proxy ;
499+
491500 // We have no connectedProxies pick first of the connected ones
492501 if ( connectedProxies . length === 0 ) {
493- return self . connectedProxies [ 0 ] ;
502+ proxy = self . connectedProxies [ 0 ] ;
503+ } else {
504+ // Get proxy
505+ proxy = connectedProxies [ self . index % connectedProxies . length ] ;
506+ // Update the index
507+ self . index = ( self . index + 1 ) % connectedProxies . length ;
508+ }
509+
510+ if ( transaction ) {
511+ transaction . pinServer ( proxy ) ;
494512 }
495513
496- // Get proxy
497- var proxy = connectedProxies [ self . index % connectedProxies . length ] ;
498- // Update the index
499- self . index = ( self . index + 1 ) % connectedProxies . length ;
500514 // Return the proxy
501515 return proxy ;
502516}
@@ -846,7 +860,7 @@ var executeWriteOperation = function(self, op, ns, ops, options, callback) {
846860 options = options || { } ;
847861
848862 // Pick a server
849- let server = pickProxy ( self ) ;
863+ let server = pickProxy ( self , options . session ) ;
850864 // No server found error out
851865 if ( ! server ) return callback ( new MongoError ( 'no mongos proxy available' ) ) ;
852866
@@ -866,7 +880,7 @@ var executeWriteOperation = function(self, op, ns, ops, options, callback) {
866880 }
867881
868882 // Pick another server
869- server = pickProxy ( self ) ;
883+ server = pickProxy ( self , options . session ) ;
870884
871885 // No server found error out with original error
872886 if ( ! server || ! isRetryableWritesSupported ( server ) ) {
@@ -1007,7 +1021,7 @@ Mongos.prototype.command = function(ns, cmd, options, callback) {
10071021 var self = this ;
10081022
10091023 // Pick a proxy
1010- var server = pickProxy ( self ) ;
1024+ var server = pickProxy ( self , options . session ) ;
10111025
10121026 // Topology is not connected, save the call in the provided store to be
10131027 // Executed at some point when the handler deems it's reconnected
@@ -1087,7 +1101,8 @@ Mongos.prototype.cursor = function(ns, cmd, options) {
10871101 *
10881102 * @method
10891103 * @param {function } selector Unused
1090- * @param {ReadPreference } [options.readPreference] Specify read preference if command supports it
1104+ * @param {ReadPreference } [options.readPreference] Unused
1105+ * @param {ClientSession } [options.session] Specify a session if it is being used
10911106 * @param {function } callback
10921107 */
10931108Mongos . prototype . selectServer = function ( selector , options , callback ) {
@@ -1097,7 +1112,7 @@ Mongos.prototype.selectServer = function(selector, options, callback) {
10971112 ( callback = options ) , ( options = selector ) , ( selector = undefined ) ;
10981113 options = options || { } ;
10991114
1100- const server = pickProxy ( this ) ;
1115+ const server = pickProxy ( this , options . session ) ;
11011116 if ( this . s . debug ) this . emit ( 'pickedServer' , null , server ) ;
11021117 callback ( null , server ) ;
11031118} ;
0 commit comments