Support Min connection pool parameter #3009 (#3438)

* Support Min connection pool parameter #3009

* Remove extraneous change

* streamline code
This commit is contained in:
Harish T 2025-04-25 12:41:02 -07:00 committed by GitHub
parent 229de3006b
commit ff40638868
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 1 deletions

View File

@ -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)

View File

@ -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()
})
)
})