@@ -93,6 +93,8 @@ export interface ServerPrivate {
93
93
pool : ConnectionPool ;
94
94
/** MongoDB server API version */
95
95
serverApi ?: ServerApi ;
96
+ /** A count of the operations currently running against the server. */
97
+ operationCount : number ;
96
98
}
97
99
98
100
/** @public */
@@ -147,7 +149,8 @@ export class Server extends TypedEventEmitter<ServerEvents> {
147
149
logger : new Logger ( 'Server' ) ,
148
150
state : STATE_CLOSED ,
149
151
topology,
150
- pool : new ConnectionPool ( poolOptions )
152
+ pool : new ConnectionPool ( poolOptions ) ,
153
+ operationCount : 0
151
154
} ;
152
155
153
156
for ( const event of [ ...CMAP_EVENTS , ...APM_EVENTS ] ) {
@@ -325,6 +328,15 @@ export class Server extends TypedEventEmitter<ServerEvents> {
325
328
// NOTE: This is a hack! We can't retrieve the connections used for executing an operation
326
329
// (and prevent them from being checked back in) at the point of operation execution.
327
330
// 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.
328
340
if ( this . loadBalanced && session && conn == null && isPinnableCommand ( cmd , session ) ) {
329
341
this . s . pool . checkOut ( ( err , checkedOut ) => {
330
342
if ( err || checkedOut == null ) {
@@ -335,14 +347,16 @@ export class Server extends TypedEventEmitter<ServerEvents> {
335
347
session . pin ( checkedOut ) ;
336
348
this . command ( ns , cmd , finalOptions , callback ) ;
337
349
} ) ;
338
-
339
350
return ;
340
351
}
341
352
353
+ this . s . operationCount += 1 ;
354
+
342
355
this . s . pool . withConnection (
343
356
conn ,
344
357
( err , conn , cb ) => {
345
358
if ( err || ! conn ) {
359
+ this . s . operationCount -= 1 ;
346
360
markServerUnknown ( this , err ) ;
347
361
return cb ( err ) ;
348
362
}
@@ -351,7 +365,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
351
365
ns ,
352
366
cmd ,
353
367
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
+ } )
355
372
) ;
356
373
} ,
357
374
callback
@@ -373,15 +390,26 @@ export class Server extends TypedEventEmitter<ServerEvents> {
373
390
return ;
374
391
}
375
392
393
+ this . s . operationCount += 1 ;
394
+
376
395
this . s . pool . withConnection (
377
396
options . session ?. pinnedConnection ,
378
397
( err , conn , cb ) => {
379
398
if ( err || ! conn ) {
399
+ this . s . operationCount -= 1 ;
380
400
markServerUnknown ( this , err ) ;
381
401
return cb ( err ) ;
382
402
}
383
403
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
+ ) ;
385
413
} ,
386
414
callback
387
415
) ;
@@ -405,10 +433,12 @@ export class Server extends TypedEventEmitter<ServerEvents> {
405
433
return ;
406
434
}
407
435
436
+ this . s . operationCount += 1 ;
408
437
this . s . pool . withConnection (
409
438
options . session ?. pinnedConnection ,
410
439
( err , conn , cb ) => {
411
440
if ( err || ! conn ) {
441
+ this . s . operationCount -= 1 ;
412
442
markServerUnknown ( this , err ) ;
413
443
return cb ( err ) ;
414
444
}
@@ -417,7 +447,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
417
447
ns ,
418
448
cursorIds ,
419
449
options ,
420
- makeOperationHandler ( this , conn , { } , undefined , cb )
450
+ makeOperationHandler ( this , conn , { } , undefined , ( error , response ) => {
451
+ this . s . operationCount -= 1 ;
452
+ cb ( error , response ) ;
453
+ } )
421
454
) ;
422
455
} ,
423
456
callback
0 commit comments