Skip to content

Commit

Permalink
Expose queue depth and pending resources in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Aug 10, 2023
1 parent 0078c48 commit 8a9baba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 19 additions & 1 deletion test/Pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8a9baba

Please sign in to comment.