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
27
27
application run faster.
28
28
29
29
The 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.
31
31
32
- Event Subscription Example
33
- --------------------------
32
+ Event Subscription Examples
33
+ ---------------------------
34
34
35
35
You can access one or more connection pool events using the driver by
36
36
subscribing to them in your application. The following example demonstrates
@@ -40,6 +40,13 @@ pool monitoring events created by the MongoDB deployment:
40
40
.. literalinclude:: /code-snippets/monitoring/cpm-subscribe.js
41
41
:language: javascript
42
42
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
+
43
50
Event Descriptions
44
51
------------------
45
52
You can’t perform that action at this time.
0 commit comments