Add retry logic to postgres requests

Maybe solves https://github.com/ajayyy/SponsorBlockServer/issues/487
This commit is contained in:
Ajay
2022-07-20 14:42:04 -04:00
parent dd47f87616
commit 96a75a8335

View File

@@ -32,6 +32,8 @@ export class Postgres implements IDatabase {
private poolRead: Pool;
private lastPoolReadFail = 0;
private maxTries = 3;
constructor(private config: DatabaseConfig) {}
async init(): Promise<void> {
@@ -94,27 +96,36 @@ export class Postgres implements IDatabase {
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
try {
const queryResult = await this.getPool(type, options).query({ text: query, values: params });
let tries = 0;
do {
tries++;
switch (type) {
case "get": {
const value = queryResult.rows[0];
Logger.debug(`result (postgres): ${JSON.stringify(value)}`);
return value;
try {
const queryResult = await this.getPool(type, options).query({ text: query, values: params });
switch (type) {
case "get": {
const value = queryResult.rows[0];
Logger.debug(`result (postgres): ${JSON.stringify(value)}`);
return value;
}
case "all": {
const values = queryResult.rows;
Logger.debug(`result (postgres): ${JSON.stringify(values)}`);
return values;
}
case "run": {
return;
}
}
case "all": {
const values = queryResult.rows;
Logger.debug(`result (postgres): ${JSON.stringify(values)}`);
return values;
}
case "run": {
break;
} catch (err) {
if (err instanceof Error && err.message.includes("terminating connection due to conflict with recovery")) {
options.useReplica = false;
}
Logger.error(`prepare (postgres) try ${tries}: ${err}`);
}
} catch (err) {
Logger.error(`prepare (postgres): ${err}`);
}
} while ((type === "get" || type === "all") && tries < this.maxTries);
}
private getPool(type: string, options: QueryOption): Pool {