File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 1+ function connectionPoolStatus ( client ) {
2+ let checkedOut = 0 ;
3+
4+ function onCheckout ( ) {
5+ checkedOut ++ ;
6+ }
7+
8+ function onCheckin ( ) {
9+ checkedOut -- ;
10+ }
11+
12+ function onClose ( ) {
13+ client . removeListener ( 'connectionCheckedOut' , onCheckout ) ;
14+ client . removeListener ( 'connectionCheckedIn' , onCheckin ) ;
15+
16+ checkedOut = NaN ;
17+ }
18+
19+ // Decreases count of connections checked out of the pool when connectionCheckedIn event is triggered
20+ client . on ( 'connectionCheckedIn' , onCheckin ) ;
21+
22+ // Increases count of connections checked out of the pool when connectionCheckedOut event is triggered
23+ client . on ( 'connectionCheckedOut' , onCheckout ) ;
24+
25+ // Cleans up event listeners when client is closed
26+ client . on ( 'close' , onClose ) ;
27+
28+ return {
29+ count : ( ) => checkedOut ,
30+ cleanUp : onClose
31+ } ;
32+ }
Original file line number Diff line number Diff line change @@ -27,10 +27,10 @@ network handshakes your application needs to perform and can help your
2727application run faster.
2828
2929The following sections demonstrate how to record connection pool events in your
30- application or want to explore the information provided in these events.
30+ application and explore the information provided in these events.
3131
32- Event Subscription Example
33- --------------------------
32+ Event Subscription Examples
33+ ---------------------------
3434
3535You can access one or more connection pool events using the driver by
3636subscribing to them in your application. The following example demonstrates
@@ -40,6 +40,13 @@ pool monitoring events created by the MongoDB deployment:
4040.. literalinclude:: /code-snippets/monitoring/cpm-subscribe.js
4141 :language: javascript
4242
43+ Connection pool monitoring events can aid you in debugging and understanding
44+ the behavior of your application's connection pool. The following example uses connection
45+ pool monitoring events to return a count of checked-out connections in the pool:
46+
47+ .. literalinclude:: /code-snippets/monitoring/cpm-status.js
48+ :language: javascript
49+
4350Event Descriptions
4451------------------
4552
You can’t perform that action at this time.
0 commit comments