Skip to content

Commit 1e66a2e

Browse files
committed
Add option to disable fallback after failure
1 parent 072324f commit 1e66a2e

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ addDefaults(config, {
8888
readTimeout: 250,
8989
max: 10,
9090
idleTimeoutMillis: 10000,
91-
maxTries: 3
91+
maxTries: 3,
92+
fallbackOnFail: true
9293
},
9394
dumpDatabase: {
9495
enabled: false,

src/databases/Postgres.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ export class Postgres implements IDatabase {
148148

149149
private getPool(type: string, options: QueryOption): Pool {
150150
const readAvailable = this.poolRead && options.useReplica && this.isReadQuery(type);
151-
const ignroreReadDueToFailure = this.lastPoolReadFail > Date.now() - 1000 * 30;
152-
const readDueToFailure = this.lastPoolFail > Date.now() - 1000 * 30;
151+
const ignroreReadDueToFailure = this.config.postgresReadOnly.fallbackOnFail
152+
&& this.lastPoolReadFail > Date.now() - 1000 * 30;
153+
const readDueToFailure = this.config.postgresReadOnly.fallbackOnFail
154+
&& this.lastPoolFail > Date.now() - 1000 * 30;
153155
if (readAvailable && !ignroreReadDueToFailure && (options.forceReplica || readDueToFailure ||
154156
Math.random() > 1 / (this.config.postgresReadOnly.weight + 1))) {
155157
return this.poolRead;

0 commit comments

Comments
 (0)