1
1
import type { ObjectId } from '../bson' ;
2
- import type { AnyError } from '../error' ;
2
+ import {
3
+ CONNECTION_CHECK_OUT_FAILED ,
4
+ CONNECTION_CHECK_OUT_STARTED ,
5
+ CONNECTION_CHECKED_IN ,
6
+ CONNECTION_CHECKED_OUT ,
7
+ CONNECTION_CLOSED ,
8
+ CONNECTION_CREATED ,
9
+ CONNECTION_POOL_CLEARED ,
10
+ CONNECTION_POOL_CLOSED ,
11
+ CONNECTION_POOL_CREATED ,
12
+ CONNECTION_POOL_READY ,
13
+ CONNECTION_READY
14
+ } from '../constants' ;
15
+ import type { MongoError } from '../error' ;
3
16
import type { Connection } from './connection' ;
4
17
import type { ConnectionPool , ConnectionPoolOptions } from './connection_pool' ;
5
18
@@ -8,11 +21,24 @@ import type { ConnectionPool, ConnectionPoolOptions } from './connection_pool';
8
21
* @public
9
22
* @category Event
10
23
*/
11
- export class ConnectionPoolMonitoringEvent {
24
+ export abstract class ConnectionPoolMonitoringEvent {
12
25
/** A timestamp when the event was created */
13
26
time : Date ;
14
27
/** The address (host/port pair) of the pool */
15
28
address : string ;
29
+ /** @internal */
30
+ abstract name :
31
+ | typeof CONNECTION_CHECK_OUT_FAILED
32
+ | typeof CONNECTION_CHECK_OUT_STARTED
33
+ | typeof CONNECTION_CHECKED_IN
34
+ | typeof CONNECTION_CHECKED_OUT
35
+ | typeof CONNECTION_CLOSED
36
+ | typeof CONNECTION_CREATED
37
+ | typeof CONNECTION_POOL_CLEARED
38
+ | typeof CONNECTION_POOL_CLOSED
39
+ | typeof CONNECTION_POOL_CREATED
40
+ | typeof CONNECTION_POOL_READY
41
+ | typeof CONNECTION_READY ;
16
42
17
43
/** @internal */
18
44
constructor ( pool : ConnectionPool ) {
@@ -29,6 +55,8 @@ export class ConnectionPoolMonitoringEvent {
29
55
export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
30
56
/** The options used to create this connection pool */
31
57
options ?: ConnectionPoolOptions ;
58
+ /** @internal */
59
+ name = CONNECTION_POOL_CREATED ;
32
60
33
61
/** @internal */
34
62
constructor ( pool : ConnectionPool ) {
@@ -43,6 +71,9 @@ export class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
43
71
* @category Event
44
72
*/
45
73
export class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent {
74
+ /** @internal */
75
+ name = CONNECTION_POOL_READY ;
76
+
46
77
/** @internal */
47
78
constructor ( pool : ConnectionPool ) {
48
79
super ( pool ) ;
@@ -55,6 +86,9 @@ export class ConnectionPoolReadyEvent extends ConnectionPoolMonitoringEvent {
55
86
* @category Event
56
87
*/
57
88
export class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
89
+ /** @internal */
90
+ name = CONNECTION_POOL_CLOSED ;
91
+
58
92
/** @internal */
59
93
constructor ( pool : ConnectionPool ) {
60
94
super ( pool ) ;
@@ -69,6 +103,8 @@ export class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
69
103
export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
70
104
/** A monotonically increasing, per-pool id for the newly created connection */
71
105
connectionId : number | '<monitor>' ;
106
+ /** @internal */
107
+ name = CONNECTION_CREATED ;
72
108
73
109
/** @internal */
74
110
constructor ( pool : ConnectionPool , connection : { id : number | '<monitor>' } ) {
@@ -85,6 +121,8 @@ export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
85
121
export class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent {
86
122
/** The id of the connection */
87
123
connectionId : number | '<monitor>' ;
124
+ /** @internal */
125
+ name = CONNECTION_READY ;
88
126
89
127
/** @internal */
90
128
constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -104,17 +142,23 @@ export class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
104
142
/** The reason the connection was closed */
105
143
reason : string ;
106
144
serviceId ?: ObjectId ;
145
+ /** @internal */
146
+ name = CONNECTION_CLOSED ;
147
+ /** @internal */
148
+ error : MongoError | null ;
107
149
108
150
/** @internal */
109
151
constructor (
110
152
pool : ConnectionPool ,
111
153
connection : Pick < Connection , 'id' | 'serviceId' > ,
112
- reason : string
154
+ reason : 'idle' | 'stale' | 'poolClosed' | 'error' ,
155
+ error ?: MongoError
113
156
) {
114
157
super ( pool ) ;
115
158
this . connectionId = connection . id ;
116
- this . reason = reason || 'unknown' ;
159
+ this . reason = reason ;
117
160
this . serviceId = connection . serviceId ;
161
+ this . error = error ?? null ;
118
162
}
119
163
}
120
164
@@ -124,6 +168,9 @@ export class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
124
168
* @category Event
125
169
*/
126
170
export class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent {
171
+ /** @internal */
172
+ name = CONNECTION_CHECK_OUT_STARTED ;
173
+
127
174
/** @internal */
128
175
constructor ( pool : ConnectionPool ) {
129
176
super ( pool ) ;
@@ -137,12 +184,21 @@ export class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEven
137
184
*/
138
185
export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent {
139
186
/** The reason the attempt to check out failed */
140
- reason : AnyError | string ;
187
+ reason : string ;
188
+ /** @internal */
189
+ error ?: MongoError ;
190
+ /** @internal */
191
+ name = CONNECTION_CHECK_OUT_FAILED ;
141
192
142
193
/** @internal */
143
- constructor ( pool : ConnectionPool , reason : AnyError | string ) {
194
+ constructor (
195
+ pool : ConnectionPool ,
196
+ reason : 'poolClosed' | 'timeout' | 'connectionError' ,
197
+ error ?: MongoError
198
+ ) {
144
199
super ( pool ) ;
145
200
this . reason = reason ;
201
+ this . error = error ;
146
202
}
147
203
}
148
204
@@ -154,6 +210,8 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
154
210
export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
155
211
/** The id of the connection */
156
212
connectionId : number | '<monitor>' ;
213
+ /** @internal */
214
+ name = CONNECTION_CHECKED_OUT ;
157
215
158
216
/** @internal */
159
217
constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -170,6 +228,8 @@ export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
170
228
export class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent {
171
229
/** The id of the connection */
172
230
connectionId : number | '<monitor>' ;
231
+ /** @internal */
232
+ name = CONNECTION_CHECKED_IN ;
173
233
174
234
/** @internal */
175
235
constructor ( pool : ConnectionPool , connection : Connection ) {
@@ -188,6 +248,8 @@ export class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
188
248
serviceId ?: ObjectId ;
189
249
190
250
interruptInUseConnections ?: boolean ;
251
+ /** @internal */
252
+ name = CONNECTION_POOL_CLEARED ;
191
253
192
254
/** @internal */
193
255
constructor (
0 commit comments