mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-09 04:57:04 +03:00
Add retry logic to postgres requests
Maybe solves https://github.com/ajayyy/SponsorBlockServer/issues/487
This commit is contained in:
@@ -32,6 +32,8 @@ export class Postgres implements IDatabase {
|
|||||||
private poolRead: Pool;
|
private poolRead: Pool;
|
||||||
private lastPoolReadFail = 0;
|
private lastPoolReadFail = 0;
|
||||||
|
|
||||||
|
private maxTries = 3;
|
||||||
|
|
||||||
constructor(private config: DatabaseConfig) {}
|
constructor(private config: DatabaseConfig) {}
|
||||||
|
|
||||||
async init(): Promise<void> {
|
async init(): Promise<void> {
|
||||||
@@ -94,6 +96,10 @@ export class Postgres implements IDatabase {
|
|||||||
|
|
||||||
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
|
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
|
||||||
|
|
||||||
|
let tries = 0;
|
||||||
|
do {
|
||||||
|
tries++;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const queryResult = await this.getPool(type, options).query({ text: query, values: params });
|
const queryResult = await this.getPool(type, options).query({ text: query, values: params });
|
||||||
|
|
||||||
@@ -109,12 +115,17 @@ export class Postgres implements IDatabase {
|
|||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
case "run": {
|
case "run": {
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(`prepare (postgres): ${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}`);
|
||||||
|
}
|
||||||
|
} while ((type === "get" || type === "all") && tries < this.maxTries);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getPool(type: string, options: QueryOption): Pool {
|
private getPool(type: string, options: QueryOption): Pool {
|
||||||
|
|||||||
Reference in New Issue
Block a user