feat(database): database connecting backoff (#2668)

This commit is contained in:
ChengLei Shao 2023-09-18 10:00:11 +08:00 committed by GitHub
parent 0f4959d217
commit a53a350f95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 14 deletions

View File

@ -19,7 +19,8 @@
"mathjs": "^10.6.1", "mathjs": "^10.6.1",
"semver": "^7.3.7", "semver": "^7.3.7",
"sequelize": "^6.26.0", "sequelize": "^6.26.0",
"umzug": "^3.1.1" "umzug": "^3.1.1",
"exponential-backoff": "^3.1.1"
}, },
"devDependencies": { "devDependencies": {
"@types/glob": "^7.2.0" "@types/glob": "^7.2.0"

View File

@ -69,6 +69,7 @@ import {
import { patchSequelizeQueryInterface, snakeCase } from './utils'; import { patchSequelizeQueryInterface, snakeCase } from './utils';
import { BaseValueParser, registerFieldValueParsers } from './value-parsers'; import { BaseValueParser, registerFieldValueParsers } from './value-parsers';
import { ViewCollection } from './view-collection'; import { ViewCollection } from './view-collection';
import { backOff } from 'exponential-backoff';
export type MergeOptions = merge.Options; export type MergeOptions = merge.Options;
@ -700,25 +701,33 @@ export class Database extends EventEmitter implements AsyncEmitter {
async auth(options: Omit<QueryOptions, 'retry'> & { retry?: number | Pick<QueryOptions, 'retry'> } = {}) { async auth(options: Omit<QueryOptions, 'retry'> & { retry?: number | Pick<QueryOptions, 'retry'> } = {}) {
const { retry = 10, ...others } = options; const { retry = 10, ...others } = options;
const delay = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)); const startingDelay = 50;
let count = 1; const timeMultiple = 2;
let attemptNumber = 1; // To track the current attempt number
const authenticate = async () => { const authenticate = async () => {
try { try {
await this.sequelize.authenticate(others); await this.sequelize.authenticate(others);
console.log('Connection has been established successfully.'); console.log('Connection has been established successfully.');
return true;
} catch (error) { } catch (error) {
console.log(`Unable to connect to the database: ${error.message}`); console.log(`Attempt ${attemptNumber}/${retry}: Unable to connect to the database: ${error.message}`);
if (count >= (retry as number)) { const nextDelay = startingDelay * Math.pow(timeMultiple, attemptNumber - 1);
throw new Error('Connection failed, please check your database connection credentials and try again.'); console.log(`Will retry in ${nextDelay}ms...`);
} attemptNumber++;
console.log('reconnecting...', count); throw error; // Re-throw the error so that backoff can catch and handle it
++count;
await delay(500);
return await authenticate();
} }
}; };
return await authenticate();
try {
await backOff(authenticate, {
numOfAttempts: retry as number,
startingDelay: startingDelay,
timeMultiple: timeMultiple,
});
} catch (error) {
throw new Error('Connection failed, please check your database connection credentials and try again.');
}
} }
async prepare() { async prepare() {

View File

@ -353,7 +353,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
return; return;
} }
this._authenticated = true; this._authenticated = true;
await this.db.auth({ retry: 30 }); await this.db.auth();
await this.dbVersionCheck({ exit: true }); await this.dbVersionCheck({ exit: true });
await this.db.prepare(); await this.db.prepare();
} }

View File

@ -11585,6 +11585,11 @@ expect@^29.6.2:
jest-message-util "^29.6.2" jest-message-util "^29.6.2"
jest-util "^29.6.2" jest-util "^29.6.2"
exponential-backoff@^3.1.1:
version "3.1.1"
resolved "https://registry.npmjs.org/exponential-backoff/-/exponential-backoff-3.1.1.tgz#64ac7526fe341ab18a39016cd22c787d01e00bf6"
integrity sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw==
ext@^1.1.2: ext@^1.1.2:
version "1.7.0" version "1.7.0"
resolved "https://registry.npmmirror.com/ext/-/ext-1.7.0.tgz#0ea4383c0103d60e70be99e9a7f11027a33c4f5f" resolved "https://registry.npmmirror.com/ext/-/ext-1.7.0.tgz#0ea4383c0103d60e70be99e9a7f11027a33c4f5f"