From 8a9babaf7724847748529bc53a2aba8d25df0f3f Mon Sep 17 00:00:00 2001 From: Steve Cresswell <229672+cressie176@users.noreply.github.com> Date: Thu, 10 Aug 2023 23:59:15 +0100 Subject: [PATCH] Expose queue depth and pending resources in stats --- lib/Pool.js | 6 +++--- test/Pool.test.js | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Pool.js b/lib/Pool.js index 2cc0e1b..00c13d7 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -74,8 +74,8 @@ module.exports = class Pool extends EventEmitter { } stats() { - const { size, idle, acquired, bad, pending, available } = this._resourceStore; - return { size, idle, acquired, bad, available, pending }; + const { size, idle, pending, acquired, bad, available } = this._resourceStore; + return { size, idle, queued: this._acquireQueue.length, pending, acquired, bad, available }; } async shutdown() { @@ -154,7 +154,7 @@ module.exports = class Pool extends EventEmitter { } _checkAcquireQueue() { - debug('[%d] Checking acquire queue', getContextId()); + debug('[%d] Checking acquire queue (%d)', getContextId(), this._acquireQueue.length); if (this._acquireQueue.length === 0) return debug('[%d] Acquire queue is empty', getContextId()); if (this._resourceStore.isExhausted()) return debug('[%d] Pool is exhausted', getContextId()); const { resolve } = this._acquireQueue.shift(); diff --git a/test/Pool.test.js b/test/Pool.test.js index 75dc6a4..2d05902 100644 --- a/test/Pool.test.js +++ b/test/Pool.test.js @@ -621,9 +621,11 @@ describe('Pool', () => { const factory = new TestFactory(); const pool = createPool({ factory }); - const { size, idle, acquired, bad, available } = pool.stats(); + const { size, idle, queued, pending, acquired, bad, available } = pool.stats(); eq(size, 0); eq(idle, 0); + eq(queued, 0); + eq(pending, 0); eq(acquired, 0); eq(bad, 0); eq(available, Infinity); @@ -660,6 +662,22 @@ describe('Pool', () => { eq(available, Infinity); }); + it('should report stats for a pool with queued acquisition requests', async () => { + const resources = ['R1']; + const factory = new TestFactory(resources); + const pool = createPool({ factory, maxSize: 1 }); + + await pool.acquire(); + pool.acquire(); + pool.acquire(); + + const { size, idle, queued, acquired } = pool.stats(); + eq(size, 1); + eq(idle, 0); + eq(queued, 2); + eq(acquired, 1); + }); + it('should report stats for a pool with bad resources', async (t, done) => { const resources = ['R1', { destroyError: 'Oh Noes!', value: 'R2' }, 'R3']; const factory = new TestFactory(resources);