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

fix(postgres): invalidate connection after client-side timeout #15144

Merged
merged 7 commits into from Oct 24, 2022

Conversation

harrykao
Copy link
Contributor

@harrykao harrykao commented Oct 17, 2022

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

The query_timeout feature of the pg package helps handle stuck TCP connections more quickly and gracefully by implementing a client-side timeout:

brianc/node-postgres#1713

Sequelize started passing this dialect-specific option through to pg here:

#13258

I believe we also want to invalidate the connection when a client-side timeout occurs. We shouldn't try to reuse the stuck connection because...it's stuck.

This PR updates the error handling code so that the connection is invalidated if the error matches the one thrown from here:

https://github.com/brianc/node-postgres/blob/5538df6b446f4b4f921947b460fe38acb897e579/packages/pg/lib/client.js#L529

The `query_timeout` feature of the `pg` package helps handle stuck TCP
connections more quickly and gracefully by implementing a client-side
timeout:

brianc/node-postgres#1713

Sequelize started passing this dialect-specific option through to `pg` here:

sequelize#13258

I believe we also want to invalidate the connection when a client-side
timeout occurs. We shouldn't try to reuse the stuck connection
because...it's stuck.

This PR updates the error handling code so that the connection is
invalidated if the error matches the one thrown from here:

https://github.com/brianc/node-postgres/blob/5538df6b446f4b4f921947b460fe38acb897e579/packages/pg/lib/client.js#L529
@harrykao
Copy link
Contributor Author

@sincerekamal Since you added the pass-through for the query_timeout option, does this change seem right to you?

@harrykao harrykao changed the title feat(postgres): invalidate connection after client-side timeout fix(postgres): invalidate connection after client-side timeout Oct 17, 2022
@harrykao
Copy link
Contributor Author

@ephys @WikiRik Sorry to bug you. What's the right way to ask someone to review this?

@WikiRik
Copy link
Member

WikiRik commented Oct 19, 2022

Hi @harrykao
This is fine. We'll take a look at this soon. Thanks for the message

Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can make a test with a very low query_timeout and a query that does take a while? Not a high priority but would be nice to see.

Apart from that I think this should not be an issue, but I do have a question about your explanation. See my comment about that

src/dialects/postgres/query.js Outdated Show resolved Hide resolved
src/dialects/postgres/query.js Outdated Show resolved Hide resolved
@WikiRik WikiRik requested a review from ephys October 20, 2022 15:46
@harrykao
Copy link
Contributor Author

@WikiRik Thanks for the review. I've added a test.

I'm not sure about coding style here and I'd be happy to do another round to make this more consistent with the rest of the code.

@harrykao
Copy link
Contributor Author

Hrm, it looks to me like the native bindings don't pass statement_timeout:

https://github.com/brianc/node-pg-native/blob/master/index.js#L14

Or implement query_timeout.

Would it make sense to skip these tests when testing against native?

@WikiRik
Copy link
Member

WikiRik commented Oct 21, 2022

I think it would be fine to skip these on postgres-native. You can just add an if-statement at the top of the describe block that just returns if the dialect is postgres-native. That reduces nesting and is our new best practice

WikiRik
WikiRik previously approved these changes Oct 23, 2022
Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small suggestion, but it's non blocking
I think it's fine having the new test functions here, especially since a developer might not directly expect that getConnectionPid also releases the connection.

src/dialects/postgres/query.js Outdated Show resolved Hide resolved
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
@WikiRik WikiRik merged commit ff43e8d into sequelize:main Oct 24, 2022
@harrykao
Copy link
Contributor Author

Thanks @WikiRik!

@harrykao harrykao deleted the invalidate_on_read_timeout branch October 24, 2022 14:07
@harrykao
Copy link
Contributor Author

@WikiRik Could I get a v6 release with this fix?

@WikiRik
Copy link
Member

WikiRik commented Nov 14, 2022

Can you open a new PR with this change towards our v6 branch?

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

Successfully merging this pull request may close these issues.

None yet

2 participants