Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: don't recreate instances when client is idle
  • Loading branch information
schmidt-sebastian committed Jan 6, 2020
1 parent 186cdba commit 0aa2a8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dev/src/pool.ts
Expand Up @@ -68,7 +68,7 @@ export class ClientPool<T> {
*/
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
Expand Down Expand Up @@ -99,6 +99,7 @@ export class ClientPool<T> {
} 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'
Expand Down
30 changes: 29 additions & 1 deletion dev/test/pool.ts
Expand Up @@ -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 {};
});
Expand Down Expand Up @@ -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<number>(2, 0, () => {
Expand Down

0 comments on commit 0aa2a8b

Please sign in to comment.