From a8787afabd411cdc8fa6dbde99a981f18d76748f Mon Sep 17 00:00:00 2001 From: Harish T Date: Wed, 23 Apr 2025 15:52:11 -0700 Subject: [PATCH 1/3] Support Min connection pool parameter #3009 --- packages/pg-pool/index.js | 29 +++++++++----- packages/pg-pool/test/sizing.js | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 0a88d829a..48e2420ec 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) { @@ -334,8 +339,10 @@ class Pool extends EventEmitter { // release a client back to the poll, include an error // to remove it from the pool - _release(client, idleListener, err) { - client.on('error', idleListener) + _release(client, idleListener, err, registerListener = true) { + if (registerListener) { + client.on('error', idleListener) + } client._poolUseCount = (client._poolUseCount || 0) + 1 @@ -363,14 +370,16 @@ class Pool extends EventEmitter { // idle timeout let tid if (this.options.idleTimeoutMillis) { - tid = setTimeout(() => { - this.log('remove idle client') - this._remove(client) - }, this.options.idleTimeoutMillis) - - if (this.options.allowExitOnIdle) { - // allow Node to exit if this is all that's left - tid.unref() + if (this._isAboveMin()) { + tid = setTimeout(() => { + this.log('remove idle client') + this._remove(client) + }, this.options.idleTimeoutMillis) + + if (this.options.allowExitOnIdle) { + // allow Node to exit if this is all that's left + tid.unref() + } } } 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() + }) + ) }) From 29ef40cf9299dabf494ad83fa678e057dc0de122 Mon Sep 17 00:00:00 2001 From: Harish T Date: Wed, 23 Apr 2025 15:53:22 -0700 Subject: [PATCH 2/3] Remove extraneous change --- packages/pg-pool/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 48e2420ec..85521e208 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -339,10 +339,8 @@ class Pool extends EventEmitter { // release a client back to the poll, include an error // to remove it from the pool - _release(client, idleListener, err, registerListener = true) { - if (registerListener) { - client.on('error', idleListener) - } + _release(client, idleListener, err) { + client.on('error', idleListener) client._poolUseCount = (client._poolUseCount || 0) + 1 From 691d576a1f294ad05f198c9574215ca98bed2a5b Mon Sep 17 00:00:00 2001 From: Harish T Date: Thu, 24 Apr 2025 10:00:47 -0700 Subject: [PATCH 3/3] streamline code --- packages/pg-pool/index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 85521e208..23a7940fe 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -367,17 +367,15 @@ class Pool extends EventEmitter { // idle timeout let tid - if (this.options.idleTimeoutMillis) { - if (this._isAboveMin()) { - tid = setTimeout(() => { - this.log('remove idle client') - this._remove(client) - }, this.options.idleTimeoutMillis) - - if (this.options.allowExitOnIdle) { - // allow Node to exit if this is all that's left - tid.unref() - } + if (this.options.idleTimeoutMillis && this._isAboveMin()) { + tid = setTimeout(() => { + this.log('remove idle client') + this._remove(client) + }, this.options.idleTimeoutMillis) + + if (this.options.allowExitOnIdle) { + // allow Node to exit if this is all that's left + tid.unref() } }