@@ -93,6 +93,8 @@ export interface ServerPrivate {
9393 pool : ConnectionPool ;
9494 /** MongoDB server API version */
9595 serverApi ?: ServerApi ;
96+ /** A count of the operations currently running against the server. */
97+ operationCount : number ;
9698}
9799
98100/** @public */
@@ -147,7 +149,8 @@ export class Server extends TypedEventEmitter<ServerEvents> {
147149 logger : new Logger ( 'Server' ) ,
148150 state : STATE_CLOSED ,
149151 topology,
150- pool : new ConnectionPool ( poolOptions )
152+ pool : new ConnectionPool ( poolOptions ) ,
153+ operationCount : 0
151154 } ;
152155
153156 for ( const event of [ ...CMAP_EVENTS , ...APM_EVENTS ] ) {
@@ -325,6 +328,15 @@ export class Server extends TypedEventEmitter<ServerEvents> {
325328 // NOTE: This is a hack! We can't retrieve the connections used for executing an operation
326329 // (and prevent them from being checked back in) at the point of operation execution.
327330 // This should be considered as part of the work for NODE-2882
331+ // NOTE:
332+ // When incrementing operation count, it's important that we increment it before we
333+ // attempt to check out a connection from the pool. This ensures that operations that
334+ // are waiting for a connection are included in the operation count. Load balanced
335+ // mode will only ever have a single server, so the operation count doesn't matter.
336+ // Incrementing the operation count above the logic to handle load balanced mode would
337+ // require special logic to decrement it again, or would double increment (the load
338+ // balanced code makes a recursive call). Instead, we increment the count after this
339+ // check.
328340 if ( this . loadBalanced && session && conn == null && isPinnableCommand ( cmd , session ) ) {
329341 this . s . pool . checkOut ( ( err , checkedOut ) => {
330342 if ( err || checkedOut == null ) {
@@ -335,14 +347,16 @@ export class Server extends TypedEventEmitter<ServerEvents> {
335347 session . pin ( checkedOut ) ;
336348 this . command ( ns , cmd , finalOptions , callback ) ;
337349 } ) ;
338-
339350 return ;
340351 }
341352
353+ this . s . operationCount += 1 ;
354+
342355 this . s . pool . withConnection (
343356 conn ,
344357 ( err , conn , cb ) => {
345358 if ( err || ! conn ) {
359+ this . s . operationCount -= 1 ;
346360 markServerUnknown ( this , err ) ;
347361 return cb ( err ) ;
348362 }
@@ -351,7 +365,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
351365 ns ,
352366 cmd ,
353367 finalOptions ,
354- makeOperationHandler ( this , conn , cmd , finalOptions , cb )
368+ makeOperationHandler ( this , conn , cmd , finalOptions , ( error , response ) => {
369+ this . s . operationCount -= 1 ;
370+ cb ( error , response ) ;
371+ } )
355372 ) ;
356373 } ,
357374 callback
@@ -373,15 +390,26 @@ export class Server extends TypedEventEmitter<ServerEvents> {
373390 return ;
374391 }
375392
393+ this . s . operationCount += 1 ;
394+
376395 this . s . pool . withConnection (
377396 options . session ?. pinnedConnection ,
378397 ( err , conn , cb ) => {
379398 if ( err || ! conn ) {
399+ this . s . operationCount -= 1 ;
380400 markServerUnknown ( this , err ) ;
381401 return cb ( err ) ;
382402 }
383403
384- conn . getMore ( ns , cursorId , options , makeOperationHandler ( this , conn , { } , options , cb ) ) ;
404+ conn . getMore (
405+ ns ,
406+ cursorId ,
407+ options ,
408+ makeOperationHandler ( this , conn , { } , options , ( error , response ) => {
409+ this . s . operationCount -= 1 ;
410+ cb ( error , response ) ;
411+ } )
412+ ) ;
385413 } ,
386414 callback
387415 ) ;
@@ -405,10 +433,12 @@ export class Server extends TypedEventEmitter<ServerEvents> {
405433 return ;
406434 }
407435
436+ this . s . operationCount += 1 ;
408437 this . s . pool . withConnection (
409438 options . session ?. pinnedConnection ,
410439 ( err , conn , cb ) => {
411440 if ( err || ! conn ) {
441+ this . s . operationCount -= 1 ;
412442 markServerUnknown ( this , err ) ;
413443 return cb ( err ) ;
414444 }
@@ -417,7 +447,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
417447 ns ,
418448 cursorIds ,
419449 options ,
420- makeOperationHandler ( this , conn , { } , undefined , cb )
450+ makeOperationHandler ( this , conn , { } , undefined , ( error , response ) => {
451+ this . s . operationCount -= 1 ;
452+ cb ( error , response ) ;
453+ } )
421454 ) ;
422455 } ,
423456 callback
0 commit comments