From ae95f7e3eaadfe984a57ae8ca305af16bf6e1c1f Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 7 Sep 2022 22:10:48 -0400 Subject: [PATCH] Only give up on replica for last retry --- src/databases/Postgres.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 07879cb..670c591 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -103,6 +103,8 @@ export class Postgres implements IDatabase { let tries = 0; let lastPool: Pool = null; + const maxTries = () => (lastPool === this.pool + ? this.config.postgres.maxTries : this.config.postgresReadOnly.maxTries); do { tries++; @@ -133,14 +135,13 @@ export class Postgres implements IDatabase { if (lastPool === this.pool) { // Only applies if it is get or all request options.forceReplica = true; - } else if (lastPool === this.poolRead) { + } else if (lastPool === this.poolRead && maxTries() - tries <= 1) { options.useReplica = false; } Logger.error(`prepare (postgres) try ${tries}: ${err}`); } - } while (this.isReadQuery(type) && tries < (lastPool === this.pool - ? this.config.postgres.maxTries : this.config.postgresReadOnly.maxTries)); + } while (this.isReadQuery(type) && tries < maxTries()); throw new Error(`prepare (postgres): ${type} ${query} failed after ${tries} tries`); }