@@ -157,6 +157,7 @@ export class PoolManager {
157157 */
158158class TenantPool implements PoolStrategy {
159159 protected pool ?: Knex
160+ protected monitorHandle ?: ReturnType < typeof setInterval >
160161
161162 constructor ( protected readonly options : TenantConnectionOptions ) { }
162163
@@ -166,10 +167,12 @@ class TenantPool implements PoolStrategy {
166167 }
167168
168169 this . pool = this . createKnexPool ( )
170+ this . startMonitor ( )
169171 return this . pool
170172 }
171173
172174 destroy ( ) : Promise < void > {
175+ this . stopMonitor ( )
173176 const originalPool = this . pool
174177
175178 if ( ! originalPool ) {
@@ -180,6 +183,27 @@ class TenantPool implements PoolStrategy {
180183 return this . drainPool ( originalPool )
181184 }
182185
186+ protected startMonitor ( ) {
187+ this . monitorHandle = setInterval ( ( ) => {
188+ const tarnPool = this . pool ?. client ?. pool
189+ if ( ! tarnPool ) return
190+
191+ dbInUseConnection . record ( tarnPool . numUsed ( ) , { tenant_id : this . options . tenantId } )
192+ dbActiveConnection . record ( tarnPool . numUsed ( ) + tarnPool . numFree ( ) , {
193+ tenant_id : this . options . tenantId ,
194+ } )
195+ } , 2000 )
196+
197+ this . monitorHandle . unref ( )
198+ }
199+
200+ protected stopMonitor ( ) {
201+ if ( this . monitorHandle ) {
202+ clearInterval ( this . monitorHandle )
203+ this . monitorHandle = undefined
204+ }
205+ }
206+
183207 getSettings ( ) {
184208 const isSingleUseExternalPool = this . options . isSingleUse && this . options . isExternalPool
185209
@@ -208,6 +232,7 @@ class TenantPool implements PoolStrategy {
208232 return
209233 }
210234
235+ this . stopMonitor ( )
211236 const originalPool = this . pool
212237
213238 this . options . clusterSize = options . clusterSize
@@ -255,9 +280,8 @@ class TenantPool implements PoolStrategy {
255280 } )
256281
257282 const maxConnections = settings . maxConnections
258- const tenantId = this . options . tenantId
259283
260- const pool = knex ( {
284+ return knex ( {
261285 client : 'pg' ,
262286 version : dbPostgresVersion ,
263287 searchPath : settings . searchPath ,
@@ -276,50 +300,5 @@ class TenantPool implements PoolStrategy {
276300 } ,
277301 acquireConnectionTimeout : databaseConnectionTimeout ,
278302 } )
279-
280- // Track total connections in pool per tenant
281- pool . client . pool . on ( 'createSuccess' , ( ) => {
282- dbActiveConnection . add ( 1 , { tenant_id : tenantId } )
283- } )
284-
285- pool . client . pool . on ( 'destroySuccess' , ( ) => {
286- dbActiveConnection . add ( - 1 , { tenant_id : tenantId } )
287- } )
288-
289- // Track in-use connections per tenant
290- pool . client . pool . on ( 'acquireSuccess' , ( ) => {
291- dbInUseConnection . add ( 1 , { tenant_id : tenantId } )
292- } )
293-
294- pool . client . pool . on ( 'release' , ( ) => {
295- dbInUseConnection . add ( - 1 , { tenant_id : tenantId } )
296- } )
297-
298- // Track connection acquisition time using eventId to correlate requests with completions
299- const pendingAcquires = new Map < number , number > ( )
300-
301- pool . client . pool . on ( 'acquireRequest' , ( eventId : number ) => {
302- pendingAcquires . set ( eventId , performance . now ( ) )
303- } )
304-
305- pool . client . pool . on ( 'acquireSuccess' , ( eventId : number ) => {
306- const startTime = pendingAcquires . get ( eventId )
307- if ( startTime !== undefined ) {
308- pendingAcquires . delete ( eventId )
309- const durationSeconds = ( performance . now ( ) - startTime ) / 1000
310- dbConnectionAcquireTime . record ( durationSeconds , { tenant_id : tenantId } )
311- }
312- } )
313-
314- pool . client . pool . on ( 'acquireFail' , ( eventId : number ) => {
315- const startTime = pendingAcquires . get ( eventId )
316- if ( startTime !== undefined ) {
317- pendingAcquires . delete ( eventId )
318- const durationSeconds = ( performance . now ( ) - startTime ) / 1000
319- dbConnectionAcquireTime . record ( durationSeconds , { tenant_id : tenantId , failed : 'true' } )
320- }
321- } )
322-
323- return pool
324303 }
325304}
0 commit comments