feat: use mysql connection pool

This commit is contained in:
Axel Cocat 2022-02-28 23:27:46 +01:00
parent 5602ece097
commit 4b8dc1fa1c
2 changed files with 9 additions and 8 deletions

View file

@ -4,7 +4,7 @@ import { Express } from "express";
import * as express from "express"; import * as express from "express";
import * as winston from "winston"; import * as winston from "winston";
import * as morgan from "morgan"; import * as morgan from "morgan";
import { Connection, createConnection } from "mysql2/promise"; import { Pool, createPool } from "mysql2/promise";
import { engine as handlebarsEngine } from "express-handlebars"; import { engine as handlebarsEngine } from "express-handlebars";
import { Config } from "./Config"; import { Config } from "./Config";
@ -17,10 +17,10 @@ export class Armory {
public characterCustomization: CharacterCustomization; public characterCustomization: CharacterCustomization;
public dbc: DbcManager; public dbc: DbcManager;
public config: Config; public config: Config;
public worldDb: Connection; public worldDb: Pool;
public logger: winston.Logger; public logger: winston.Logger;
private charsDbs: { [key: string]: Connection }; private charsDbs: { [key: string]: Pool };
private errorNames: { [key: number]: string }; private errorNames: { [key: number]: string };
private errorDescriptions: { [key: number]: string }; private errorDescriptions: { [key: number]: string };
@ -68,9 +68,9 @@ export class Armory {
await this.characterCustomization.loadData(); await this.characterCustomization.loadData();
this.logger.info("Connecting to databases..."); this.logger.info("Connecting to databases...");
this.worldDb = await createConnection(this.config.worldDatabase); this.worldDb = createPool(this.config.worldDatabase);
for (const realm of this.config.realms) { for (const realm of this.config.realms) {
this.charsDbs[realm.name.toLowerCase()] = await createConnection(realm.charactersDatabase); this.charsDbs[realm.name.toLowerCase()] = createPool(realm.charactersDatabase);
} }
this.logger.info("Starting server..."); this.logger.info("Starting server...");
@ -168,7 +168,7 @@ export class Armory {
}); });
} }
public getCharactersDb(realm: string): Connection { public getCharactersDb(realm: string): Pool {
return this.charsDbs[realm.toLowerCase()]; return this.charsDbs[realm.toLowerCase()];
} }

View file

@ -54,20 +54,21 @@ export class IndexController {
} }
const db = this.armory.getCharactersDb(realm.name); const db = this.armory.getCharactersDb(realm.name);
const conn = await db.getConnection();
if (!(realm.name in this.charsetCache)) { if (!(realm.name in this.charsetCache)) {
let [rows, fields] = await db.query(` let [rows, fields] = await db.query(`
SELECT CCSA.character_set_name FROM information_schema.\`TABLES\` T, SELECT CCSA.character_set_name FROM information_schema.\`TABLES\` T,
information_schema.\`COLLATION_CHARACTER_SET_APPLICABILITY\` CCSA information_schema.\`COLLATION_CHARACTER_SET_APPLICABILITY\` CCSA
WHERE CCSA.collation_name = T.table_collation WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "${db.config.database}" AND T.table_schema = "${conn.config.database}"
AND T.table_name = "characters" AND T.table_name = "characters"
`); `);
this.charsetCache[realm.name] = rows[0].character_set_name; this.charsetCache[realm.name] = rows[0].character_set_name;
} }
const charSet = this.charsetCache[realm.name]; const charSet = this.charsetCache[realm.name];
let ssp = new DataTablesSsp(req.query, db, "characters", "guid", [ let ssp = new DataTablesSsp(req.query, conn, "characters", "guid", [
{ name: "name", collation: `${charSet}_general_ci` }, { name: "name", collation: `${charSet}_general_ci` },
{ table: "guild", name: "name" }, { table: "guild", name: "name" },
{ name: "level" }, { name: "level" },