Skip to content

feat(pg-pool): Optimize client retrieval from pool by prioritizing free client with cached prepared statements #1

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 8 additions & 5 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Pool extends EventEmitter {
return this._clients.length >= this.options.max
}

_pulseQueue() {
_pulseQueue(name) {
this.log('pulse queue')
if (this.ended) {
this.log('pulse queue ended')
Expand Down Expand Up @@ -142,7 +142,10 @@ class Pool extends EventEmitter {
}
const pendingItem = this._pendingQueue.shift()
if (this._idle.length) {
const idleItem = this._idle.pop()
let idleItem = name ? this._idle.find((item) => item.client.connection.parsedStatements[name]) : null
if (!idleItem) {
idleItem = this._idle.pop()
}
clearTimeout(idleItem.timeoutId)
const client = idleItem.client
client.ref && client.ref()
Expand All @@ -168,7 +171,7 @@ class Pool extends EventEmitter {
this.emit('remove', client)
}

connect(cb) {
connect(cb, name) {
if (this.ending) {
const err = new Error('Cannot use a pool after calling end on the pool')
return cb ? cb(err) : this.Promise.reject(err)
Expand All @@ -181,7 +184,7 @@ class Pool extends EventEmitter {
if (this._isFull() || this._idle.length) {
// if we have idle clients schedule a pulse immediately
if (this._idle.length) {
process.nextTick(() => this._pulseQueue())
process.nextTick(() => this._pulseQueue(name))
}

if (!this.options.connectionTimeoutMillis) {
Expand Down Expand Up @@ -431,7 +434,7 @@ class Pool extends EventEmitter {
client.release(err)
return cb(err)
}
})
}, text.name)
return response.result
}

Expand Down