From f4b66d30ec27e811ca13f69e19598e819f44e95b Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 20 Jul 2022 00:31:51 -0400 Subject: [PATCH] Use pool query since this should catch idle errors https://github.com/brianc/node-postgres/issues/1324#issuecomment-623311914 Helps with https://github.com/ajayyy/SponsorBlockServer/issues/487 --- src/databases/Postgres.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 2d2e925..5cf1f7e 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -94,10 +94,8 @@ export class Postgres implements IDatabase { Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`); - let client: PoolClient; try { - client = await this.getClient(type); - const queryResult = await client.query({ text: query, values: params }); + const queryResult = await this.getPool(type).query({ text: query, values: params }); switch (type) { case "get": { @@ -116,24 +114,18 @@ export class Postgres implements IDatabase { } } catch (err) { Logger.error(`prepare (postgres): ${err}`); - } finally { - try { - client?.release(); - } catch (err) { - Logger.error(`prepare (postgres): ${err}`); - } } } - private getClient(type: string): Promise { + private getPool(type: string): Pool { const readAvailable = this.poolRead && (type === "get" || type === "all"); const ignroreReadDueToFailure = this.lastPoolReadFail > Date.now() - 1000 * 30; const readDueToFailure = this.lastPoolFail > Date.now() - 1000 * 30; if (readAvailable && !ignroreReadDueToFailure && (readDueToFailure || Math.random() > 1 / (this.config.postgresReadOnly.weight + 1))) { - return this.poolRead.connect(); + return this.poolRead; } else { - return this.pool.connect(); + return this.pool; } }