From 3f30789f949894bdea9f12de4ecd613703367c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Tkaczy=C5=84ski?= Date: Tue, 28 Jul 2026 17:50:25 +0200 Subject: [PATCH] fix: apply keepalive and timeouts to all database connections We have seen cases of hung TCP connections freezing indexer forever. To address that we should have a fail-safe on client side to kill such connections and retry. --- src/knex.ts | 6 +++++- src/pg.ts | 7 +------ test/unit/knex.test.ts | 6 +++++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/knex.ts b/src/knex.ts index 93c9aa7..395240b 100644 --- a/src/knex.ts +++ b/src/knex.ts @@ -70,7 +70,11 @@ export function getConnectionData(connectionString: string) { password: connectionConfig.password, host: connectionConfig.hosts[0].name, port: connectionConfig.hosts[0].port, - ssl: Object.keys(sslConfig).length > 0 ? sslConfig : undefined + ssl: Object.keys(sslConfig).length > 0 ? sslConfig : undefined, + keepAlive: true, + keepAliveInitialDelayMillis: 10_000, + connectionTimeoutMillis: 30_000, + query_timeout: 60_000 } }; } diff --git a/src/pg.ts b/src/pg.ts index 9c2c4bb..fe71d31 100644 --- a/src/pg.ts +++ b/src/pg.ts @@ -9,10 +9,5 @@ import { getConnectionData } from './knex'; export const createPgPool = (connectionString: string): Pool => { const { connection } = getConnectionData(connectionString); - const config = { - ...connection, - connectionTimeoutMillis: 30000 // 30 seconds - }; - - return new Pool(config); + return new Pool(connection); }; diff --git a/test/unit/knex.test.ts b/test/unit/knex.test.ts index aa6b011..c410d6a 100644 --- a/test/unit/knex.test.ts +++ b/test/unit/knex.test.ts @@ -15,7 +15,11 @@ describe('createKnexConfig', () => { password: 'default_password', port: 3306, ssl: undefined, - user: 'root' + user: 'root', + keepAlive: true, + keepAliveInitialDelayMillis: 10_000, + connectionTimeoutMillis: 30_000, + query_timeout: 60_000 } }); });