mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
* Support Min connection pool parameter #3009 * Remove extraneous change * streamline code
This commit is contained in:
parent
229de3006b
commit
ff40638868
@ -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)
|
||||
|
||||
@ -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()
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user