-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add connection lifetime limit option and tests #2698
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
const co = require('co') | ||
const expect = require('expect.js') | ||
|
||
const describe = require('mocha').describe | ||
const it = require('mocha').it | ||
const path = require('path') | ||
|
||
const Pool = require('../') | ||
|
||
describe('lifetime timeout', () => { | ||
it('connection lifetime should expire and remove the client', (done) => { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
pool.query('SELECT NOW()') | ||
pool.on('remove', () => { | ||
console.log('expired while idle - on-remove event') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
done() | ||
}) | ||
}) | ||
it('connection lifetime should expire and remove the client after the client is done working', (done) => { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
pool.query('SELECT pg_sleep(1.01)') | ||
pool.on('remove', () => { | ||
console.log('expired while busy - on-remove event') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
done() | ||
}) | ||
}) | ||
it('can remove expired clients and recreate them', | ||
co.wrap(function* () { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
let query = pool.query('SELECT pg_sleep(1)') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(1) | ||
yield query | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
yield pool.query('SELECT NOW()') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(1) | ||
}) | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of missing unref, this effectively disables
allowExitOnIdle
feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the unit test need to be updated? The test passed.
✓ unrefs the connections and timeouts so the program can exit when idle when the allowExitOnIdle option is set (151ms)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
maxLifetimeSeconds
should be set there to some higher value, e.g. 60.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think an additional test is needed in idle-timeout.js since
allowExitOnIdle
should be tested under both conditions (maxLifetimeSeconds
is disabled andmaxLifetimeSeconds
is a longer time period thanidleTimeoutMillis
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to also comment on this, but this is preventing a clean shutdown even when
client.end()
is called.Can't we just use
.unref()
on the timeout orMaybe even both
Edit: Something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably worth noting that the default setting for maxLifetimeSeconds is 0 (aka: disabled) and none of these issues appear unless you set it to a non-zero value. That doesn't resolve the issues, but it is important to be aware of since there is a simple workaround (set it back to 0.) It seems like the need to utilize maxLifetimeSeconds would be minimal in cases where clients are being explicitly disconnected (the feature was added primarily to support long-running services in load-balanced DB environments.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly my use case (for faster recovery after a failover to a replica) 😊
I just ran into some issue with some local scripts, we're I'm using the same config. I might do a PR to fix it in the next few days, unless anybody else is on it. But haven't seen any PRs.