Skip to content
Closed
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
this.options.maxUses = this.options.maxUses || Infinity
this.options.allowExitOnIdle = this.options.allowExitOnIdle || false
this.options.maxLifetimeSeconds = this.options.maxLifetimeSeconds || 0
this.options.retryOnTimeout = this.options.retryOnTimeout || false
this.options.maxRetries = this.options.maxRetries || 3
this.options.retryDelay = this.options.retryDelay || 3_000
this.log = this.options.log || function () {}
this.Client = this.options.Client || Client || require('pg').Client
this.Promise = this.options.Promise || global.Promise
Expand Down Expand Up @@ -229,7 +232,7 @@
return result
}

newClient(pendingItem) {
newClient(pendingItem, retryAttempt = 0) {
const client = new this.Client(this.options)
this._clients.push(client)
const idleListener = makeIdleListener(this, client)
Expand Down Expand Up @@ -258,6 +261,19 @@
this.log('client failed to connect', err)
// remove the dead client from our list of clients
this._clients = this._clients.filter((c) => c !== client)

// Retry on timeout if enabled
if (timeoutHit && this.options.retryOnTimeout && retryAttempt < this.options.maxRetries) {
this.log(`Connection timeout, retry ${retryAttempt + 1}/${this.options.maxRetries}`)

// delay the retry to avoid tight loops
setTimeout(() => {
this.newClient(pendingItem, retryAttempt + 1)
}, this.options.retryDelay)

return
}

Check failure on line 276 in packages/pg-pool/index.js

View workflow job for this annotation

GitHub Actions / lint

Delete `········`
Comment thread
MathieuGuillet marked this conversation as resolved.
Outdated
if (timeoutHit) {
err = new Error('Connection terminated due to connection timeout', { cause: err })
}
Expand Down
Loading