diff --git a/src/config.ts b/src/config.ts index 96f65de..39debed 100644 --- a/src/config.ts +++ b/src/config.ts @@ -88,7 +88,8 @@ addDefaults(config, { readTimeout: 250, max: 10, idleTimeoutMillis: 10000, - maxTries: 3 + maxTries: 3, + fallbackOnFail: true }, dumpDatabase: { enabled: false, diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 670c591..715ecb7 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -148,8 +148,10 @@ export class Postgres implements IDatabase { private getPool(type: string, options: QueryOption): Pool { const readAvailable = this.poolRead && options.useReplica && this.isReadQuery(type); - const ignroreReadDueToFailure = this.lastPoolReadFail > Date.now() - 1000 * 30; - const readDueToFailure = this.lastPoolFail > Date.now() - 1000 * 30; + const ignroreReadDueToFailure = this.config.postgresReadOnly.fallbackOnFail + && this.lastPoolReadFail > Date.now() - 1000 * 30; + const readDueToFailure = this.config.postgresReadOnly.fallbackOnFail + && this.lastPoolFail > Date.now() - 1000 * 30; if (readAvailable && !ignroreReadDueToFailure && (options.forceReplica || readDueToFailure || Math.random() > 1 / (this.config.postgresReadOnly.weight + 1))) { return this.poolRead;