Skip to content
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

Connection TTL #2965

Open
tmszdmsk opened this issue Apr 24, 2023 · 1 comment
Open

Connection TTL #2965

tmszdmsk opened this issue Apr 24, 2023 · 1 comment

Comments

@tmszdmsk
Copy link

With the newly added release event in Pool, it is possible to implement connection TTL. We did that internally in our project but it would be great to have OOTB support for it in node-postgres.

It helped us resolve problems with AZ failovers when connecting to AWS RDS when the domain name starts pointing to different machine but connections in the pool still use previous address.

I wanted to understand whether there is any need for that before I start working on the PR. What do you think?

@sehrope
Copy link
Contributor

sehrope commented Apr 24, 2023

There's a maxLifetimeSeconds property that's unfortunately not documented in the README. If set, it's a max duration in seconds to keep the client around. Here's the property:

if (this.options.maxLifetimeSeconds !== 0) {
const maxLifetimeTimeout = setTimeout(() => {
this.log('ending client due to expired lifetime')
this._expired.add(client)
const idleIndex = this._idle.findIndex((idleItem) => idleItem.client === client)
if (idleIndex !== -1) {
this._acquireClient(
client,
new PendingItem((err, client, clientRelease) => clientRelease()),
idleListener,
false
)
}
}, this.options.maxLifetimeSeconds * 1000)
maxLifetimeTimeout.unref()
client.once('end', () => clearTimeout(maxLifetimeTimeout))
}
return this._acquireClient(client, pendingItem, idleListener, true)
}
})

Here's the PR that added it: #2698

It might cover the use case you are describing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants