From 9e2e1343da552c8dcb33416b9930ad1b6f4450f3 Mon Sep 17 00:00:00 2001 From: Ajay Date: Tue, 15 Nov 2022 14:19:15 -0500 Subject: [PATCH] Add timeout for all postgres requests --- src/config.ts | 3 ++- src/databases/Postgres.ts | 1 + src/types/config.model.ts | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 3c28819..5ea715e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -77,7 +77,8 @@ addDefaults(config, { max: 10, idleTimeoutMillis: 10000, maxTries: 3, - maxActiveRequests: 0 + maxActiveRequests: 0, + timeout: 60000 }, postgresReadOnly: { enabled: false, diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 05ebb4c..a6dcceb 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -125,6 +125,7 @@ export class Postgres implements IDatabase { pendingQueries.push(savePromiseState(lastPool.query({ text: query, values: params }))); const currentPromises = [...pendingQueries]; if (options.useReplica && maxTries() - tries > 1) currentPromises.push(savePromiseState(timeoutPomise(this.config.postgresReadOnly.readTimeout))); + else if (this.config.postgres.timeout) currentPromises.push(savePromiseState(timeoutPomise(this.config.postgres.timeout))); const queryResult = await nextFulfilment(currentPromises); this.activePostgresRequests--; diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 8b06c56..459d231 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -21,6 +21,7 @@ export interface CustomPostgresConfig extends PoolConfig { export interface CustomWritePostgresConfig extends CustomPostgresConfig { maxActiveRequests: number; + timeout: number; } export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {