diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 0a88d829a..23a7940fe 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -87,6 +87,7 @@ class Pool extends EventEmitter { } this.options.max = this.options.max || this.options.poolSize || 10 + this.options.min = this.options.min || 0 this.options.maxUses = this.options.maxUses || Infinity this.options.allowExitOnIdle = this.options.allowExitOnIdle || false this.options.maxLifetimeSeconds = this.options.maxLifetimeSeconds || 0 @@ -111,6 +112,10 @@ class Pool extends EventEmitter { return this._clients.length >= this.options.max } + _isAboveMin() { + return this._clients.length > this.options.min + } + _pulseQueue() { this.log('pulse queue') if (this.ended) { @@ -362,7 +367,7 @@ class Pool extends EventEmitter { // idle timeout let tid - if (this.options.idleTimeoutMillis) { + if (this.options.idleTimeoutMillis && this._isAboveMin()) { tid = setTimeout(() => { this.log('remove idle client') this._remove(client) diff --git a/packages/pg-pool/test/sizing.js b/packages/pg-pool/test/sizing.js index e7863ba07..c237995a8 100644 --- a/packages/pg-pool/test/sizing.js +++ b/packages/pg-pool/test/sizing.js @@ -55,4 +55,72 @@ describe('pool size of 1', () => { return yield pool.end() }) ) + + it( + 'does not remove clients when at or below min', + co.wrap(function* () { + const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10 }) + const client = yield pool.connect() + client.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + expect(pool.idleCount).to.equal(1) + return yield pool.end() + }) + ) + + it( + 'does remove clients when at or below min if maxUses is reached', + co.wrap(function* () { + const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10, maxUses: 1 }) + const client = yield pool.connect() + client.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + expect(pool.idleCount).to.equal(0) + return yield pool.end() + }) + ) + + it( + 'does remove clients when at or below min if maxLifetimeSeconds is reached', + co.wrap(function* () { + const pool = new Pool({ max: 1, min: 1, idleTimeoutMillis: 10, maxLifetimeSeconds: 1 }) + const client = yield pool.connect() + client.release() + yield new Promise((resolve) => setTimeout(resolve, 1020)) + expect(pool.idleCount).to.equal(0) + return yield pool.end() + }) + ) +}) + +describe('pool size of 2', () => { + it( + 'does not remove clients when at or below min', + co.wrap(function* () { + const pool = new Pool({ max: 2, min: 2, idleTimeoutMillis: 10 }) + const client = yield pool.connect() + const client2 = yield pool.connect() + client.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + client2.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + expect(pool.idleCount).to.equal(2) + return yield pool.end() + }) + ) + + it( + 'does remove clients when above min', + co.wrap(function* () { + const pool = new Pool({ max: 2, min: 1, idleTimeoutMillis: 10 }) + const client = yield pool.connect() + const client2 = yield pool.connect() + client.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + client2.release() + yield new Promise((resolve) => setTimeout(resolve, 20)) + expect(pool.idleCount).to.equal(1) + return yield pool.end() + }) + ) })