update
This commit is contained in:
1 parent
9b651f1cd6
commit
a445fef521
21 files changed
+3161
-664
No files matched your search
+55
-11
@@ -1,14 +1,58 @@
|
||||
import mysql from "mysql2/promise";
|
||||
import { config } from "./config.js";
|
||||
|
||||
export const pool = mysql.createPool({
|
||||
host: config.dbHost,
|
||||
port: config.dbPort,
|
||||
user: config.dbUser,
|
||||
password: config.dbPassword,
|
||||
database: config.dbName,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
namedPlaceholders: true,
|
||||
multipleStatements: true,
|
||||
});
|
||||
function createPool(password) {
|
||||
return mysql.createPool({
|
||||
host: config.dbHost,
|
||||
port: config.dbPort,
|
||||
user: config.dbUser,
|
||||
password,
|
||||
database: config.dbName,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
namedPlaceholders: true,
|
||||
multipleStatements: true,
|
||||
charset: "utf8mb4",
|
||||
});
|
||||
}
|
||||
|
||||
const candidatePasswords = [...new Set([config.dbPassword, config.dbPasswordFallback].filter(Boolean))];
|
||||
let activePassword = candidatePasswords[0];
|
||||
let activePool = createPool(activePassword);
|
||||
|
||||
function isAccessDenied(error) {
|
||||
return error?.code === "ER_ACCESS_DENIED_ERROR";
|
||||
}
|
||||
|
||||
async function query(sql, values) {
|
||||
try {
|
||||
return await activePool.query(sql, values);
|
||||
} catch (error) {
|
||||
if (!isAccessDenied(error) || candidatePasswords.length < 2) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
for (const password of candidatePasswords) {
|
||||
if (password === activePassword) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const fallbackPool = createPool(password);
|
||||
try {
|
||||
const result = await fallbackPool.query(sql, values);
|
||||
activePool = fallbackPool;
|
||||
activePassword = password;
|
||||
return result;
|
||||
} catch (fallbackError) {
|
||||
await fallbackPool.end().catch(() => undefined);
|
||||
if (!isAccessDenied(fallbackError)) {
|
||||
throw fallbackError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export const pool = { query };
|
||||
Reference in new issue
Block a user