diff --git a/dev/src/pool.ts b/dev/src/pool.ts index 1e2d51123..3d417d8db 100644 --- a/dev/src/pool.ts +++ b/dev/src/pool.ts @@ -68,7 +68,7 @@ export class ClientPool { */ private acquire(requestTag: string): T { let selectedClient: T | null = null; - let selectedClientRequestCount = 0; + let selectedClientRequestCount = -1; for (const [client, requestCount] of this.activeClients) { // Use the "most-full" client that can still accommodate the request @@ -99,6 +99,7 @@ export class ClientPool { } else { logger('ClientPool.acquire', requestTag, 'Creating a new client'); selectedClient = this.clientFactory(); + selectedClientRequestCount = 0; assert( !this.activeClients.has(selectedClient), 'The provided client factory returned an existing instance' diff --git a/dev/test/pool.ts b/dev/test/pool.ts index 0ff986a4f..dfa658e6f 100644 --- a/dev/test/pool.ts +++ b/dev/test/pool.ts @@ -51,7 +51,7 @@ describe('Client pool', () => { expect(clientPool.size).to.equal(2); }); - it('re-uses idle instances', () => { + it('re-uses instances with remaining capacity', () => { const clientPool = new ClientPool<{}>(2, 0, () => { return {}; }); @@ -80,6 +80,34 @@ describe('Client pool', () => { }); }); + it('re-uses idle instances', async () => { + let instanceCount = 0; + const clientPool = new ClientPool<{}>(1, 1, () => { + ++instanceCount; + return {}; + }); + + const operationPromises = deferredPromises(2); + + let completionPromise = clientPool.run( + REQUEST_TAG, + () => operationPromises[0].promise + ); + expect(clientPool.size).to.equal(1); + operationPromises[0].resolve(); + await completionPromise; + + completionPromise = clientPool.run( + REQUEST_TAG, + () => operationPromises[1].promise + ); + expect(clientPool.size).to.equal(1); + operationPromises[1].resolve(); + await completionPromise; + + expect(instanceCount).to.equal(1); + }); + it('bin packs operations', async () => { let clientCount = 0; const clientPool = new ClientPool(2, 0, () => {