From 6be2f96f2647da2c1f152f07b0d40f1a450d8798 Mon Sep 17 00:00:00 2001 From: Ajay Date: Sat, 20 Aug 2022 17:57:40 -0400 Subject: [PATCH] Make max tries configurable --- src/config.ts | 6 ++++-- src/databases/Postgres.ts | 5 ++--- src/types/config.model.ts | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 79e9c76..936aa22 100644 --- a/src/config.ts +++ b/src/config.ts @@ -81,7 +81,8 @@ addDefaults(config, { password: "", port: 5432, max: 10, - idleTimeoutMillis: 10000 + idleTimeoutMillis: 10000, + maxTries: 3 }, postgresReadOnly: { enabled: false, @@ -92,7 +93,8 @@ addDefaults(config, { port: 5432, readTimeout: 250, max: 10, - idleTimeoutMillis: 10000 + idleTimeoutMillis: 10000, + maxTries: 3 }, dumpDatabase: { enabled: false, diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 0d2e097..35d9eaa 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -33,8 +33,6 @@ export class Postgres implements IDatabase { private poolRead: Pool; private lastPoolReadFail = 0; - private maxTries = 3; - constructor(private config: DatabaseConfig) {} async init(): Promise { @@ -141,7 +139,8 @@ export class Postgres implements IDatabase { Logger.error(`prepare (postgres) try ${tries}: ${err}`); } - } while (this.isReadQuery(type) && tries < this.maxTries); + } while (this.isReadQuery(type) && tries < (lastPool === this.pool + ? this.config.postgres.maxTries : this.config.postgresReadOnly.maxTries)); } private getPool(type: string, options: QueryOption): Pool { diff --git a/src/types/config.model.ts b/src/types/config.model.ts index da01d60..53b39cd 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -8,6 +8,7 @@ interface RedisConfig extends redis.RedisClientOptions { export interface CustomPostgresConfig extends PoolConfig { enabled: boolean; + maxTries: number; } export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {