Skip to content

Support Min connection pool parameter #3009 #3438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions packages/pg-pool/test/sizing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
)
})