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

feat(core): Make Postgres connection timeout configurable #10670

Merged
merged 3 commits into from
Sep 5, 2024

Conversation

ivov
Copy link
Contributor

@ivov ivov commented Sep 4, 2024

Set a configurable connection timeout for Postgres, to more gracefully handle network issues leading to reconnection attempts.

netroy
netroy previously approved these changes Sep 4, 2024
Copy link
Member

@netroy netroy left a comment

Choose a reason for hiding this comment

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

besides one comment, LGTM

packages/@n8n/config/src/configs/database.config.ts Outdated Show resolved Hide resolved
Copy link

cypress bot commented Sep 4, 2024

n8n    Run #6739

Run Properties:  status check passed Passed #6739  •  git commit 60a8f20fc9: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Project n8n
Branch Review postgres-connection-timeout
Run status status check passed Passed #6739
Run duration 04m 44s
Commit git commit 60a8f20fc9: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 ivov 🗃️ e2e/*
Committer Iván Ovejero
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 1
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 424
View all changes introduced in this branch ↗︎

Copy link
Contributor

github-actions bot commented Sep 4, 2024

✅ All Cypress E2E specs passed

@ivov ivov requested a review from netroy September 4, 2024 15:49
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Sep 4, 2024
Copy link
Contributor

github-actions bot commented Sep 4, 2024

✅ All Cypress E2E specs passed

@ivov ivov merged commit 8154031 into master Sep 5, 2024
37 of 38 checks passed
@ivov ivov deleted the postgres-connection-timeout branch September 5, 2024 07:31
@@ -75,6 +75,10 @@ class PostgresConfig {
@Env('DB_POSTGRESDB_POOL_SIZE')
poolSize: number = 2;

/** Postgres connection timeout (ms) */
@Env('DB_POSTGRESDB_CONNECTION_TIMEOUT')
connectionTimeoutMs: number = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

1s is a very short default value. Something like 10-30s would make more sense. Also remember to update docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to Adi already 5s was too long? #10670 (comment)

The docs PR is already linked above: n8n-io/n8n-docs#2452

Copy link
Member

Choose a reason for hiding this comment

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

typeorm docs say

The milliseconds before a timeout occurs during the initial connection to the postgres server.

So this is only the timeout for opening the connection. If that takes longer than 1 second, we might have many other issues in the setup. Setting this to 30s might not be a great idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

typeorm passes that into connectionTimeoutMillis for pg lib's Pool. So it includes the whole TCP connection opening, TLS handshake, PG wire protocol including authentication etc. I fear users might run into bunch of issues if the default is very tight

Copy link
Member

Choose a reason for hiding this comment

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

it includes the whole TCP connection opening, TLS handshake, PG wire protocol including authentication

all of that should not take more than a second, even under heavy load. If it does, there are fundamental infrastructure issues that need to be addressed, instead of letting all of this take 30 seconds before timing out.

Copy link
Contributor

@tomi tomi Sep 5, 2024

Choose a reason for hiding this comment

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

You assume everyone is running n8n with colocated DB. And even in that case the connection can take longer if the DB is under load. I can't start n8n on my local machine if I'm using a PG database that is located in Azure West Germany:

[Node] Error: Connection terminated due to connection timeout
[Node]     at Connection.<anonymous> (/n8n/node_modules/.pnpm/[email protected]/node_modules/pg/lib/client.js:132:73)
[Node]     at Object.onceWrapper (node:events:633:28)
[Node]     at Connection.emit (node:events:519:28)
[Node]     at Connection.emit (node:domain:488:12)
[Node]     at Socket.<anonymous> (/n8n/node_modules/.pnpm/[email protected]/node_modules/pg/lib/connection.js:63:12)
[Node]     at Socket.emit (node:events:519:28)
[Node]     at Socket.emit (node:domain:488:12)
[Node]     at TCP.<anonymous> (node:net:338:12) undefined
[Node] [nodemon] app crashed - waiting for file changes before starting...

Not saying it needs to be 30s, but even something like 5s would be more reasonable

Copy link
Member

Choose a reason for hiding this comment

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

Well, the question is do we really want to support a scenario like this with large latencies by default, or if anyone with a setup like this should have to manually set DB_POSTGRESDB_CONNECTION_TIMEOUT to a much higher value?

IMO, we should officially only support colocated databases, to avoid having to support all the other issues that large latencies might cause.

@github-actions github-actions bot mentioned this pull request Sep 5, 2024
@janober
Copy link
Member

janober commented Sep 5, 2024

Got released with [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants