Add option to disable fallback after failure

This commit is contained in:
Ajay
2022-09-08 15:02:56 -04:00
parent 072324f0ab
commit 1e66a2e57a
2 changed files with 6 additions and 3 deletions

View File

@@ -88,7 +88,8 @@ addDefaults(config, {
readTimeout: 250,
max: 10,
idleTimeoutMillis: 10000,
maxTries: 3
maxTries: 3,
fallbackOnFail: true
},
dumpDatabase: {
enabled: false,

View File

@@ -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;