Optimize MySQL connection pools & add Docker healthcheck with graceful queue flush (#21, #12)

- Unify MySQL pools in src/db/PoolManager.mjs shared across Settings, TrackOptions, LastFm, Dashboard, and PlayerStateStore
- Support global config.mysql.connectionLimit (default 15), preventing connection starvation
- Add gracefulShutdown() in Bot.mjs to flush active player queues and playback positions to MySQL on SIGTERM/SIGINT
- Update shard.mjs to forward SIGTERM and await shard worker queue flushes before exit
- Add event-loop responsiveness healthcheck in docker-compose.yml and Dockerfile via heartbeat probe
- Document connectionLimit and queue persistence in README and config templates
This commit is contained in:
NoLogicAlan committed 2026-10-22 01:37:42 +03:00
1 parent 665be29a16
commit 90a53f047f
13 files changed
+303 -84

No files matched your search

+8 -3
View File
@@ -2,6 +2,7 @@
import mysql from "mysql2";
import { logger } from "../core/Logger.mjs";
import { MYSQL_POOL_DEFAULTS, attachMysqlPoolGuard } from "../db/MysqlGuard.mjs";
import { getSharedMysqlPool } from "../db/PoolManager.mjs";
const DEFAULT_ALIAS = "default";
const MAX_ALIAS_LEN = 32;
@@ -23,11 +24,15 @@ export class TrackOptionsManager {
/** @private @type {number} */
_cacheMaxSize = 2000;
/** @param {object} mysqlConfig - MySQL connection config. */
/** @param {object} mysqlConfig - MySQL connection config or shared Pool instance. */
constructor(mysqlConfig) {
this.db = mysql.createPool({ ...MYSQL_POOL_DEFAULTS, connectionLimit: 10, ...mysqlConfig });
if (mysqlConfig && typeof mysqlConfig.query === "function") {
this.db = mysqlConfig;
} else {
this.db = getSharedMysqlPool(mysqlConfig);
}
attachMysqlPoolGuard(this.db, "TrackOptions");
this.db.on("error", (err) => {
this.db.on?.("error", (err) => {
logger.error("[TrackOptions] MySQL pool error:", err.code ?? err.message);
});
this._readyPromise = this._ensureTable();
+8 -9
View File
@@ -15,6 +15,7 @@ import { normalizeTrackText } from "./constants.mjs";
import { applyMixins } from "../../utils/mixins.mjs";
import { parseLastFmUrl, isLastFmUrl } from "./urlUtils.mjs";
import { MYSQL_POOL_DEFAULTS, attachMysqlPoolGuard } from "../../db/MysqlGuard.mjs";
import { getSharedPromisePool } from "../../db/PoolManager.mjs";
import UserStoreMixin from "./UserStoreMixin.mjs";
import ScrobblingMixin from "./ScrobblingMixin.mjs";
import TrackQueriesMixin from "./TrackQueriesMixin.mjs";
@@ -139,15 +140,13 @@ export class LastFmManager {
/** @private @async Get or create the MySQL connection pool. @returns {Promise<object>} */
async _getPool() {
if (this._pool) return this._pool;
const mysql = await import("mysql2/promise");
this._pool = mysql.createPool({
...MYSQL_POOL_DEFAULTS,
host: this._mysqlConfig.host,
port: this._mysqlConfig.port ?? 3306,
user: this._mysqlConfig.user,
password: this._mysqlConfig.password,
database: this._mysqlConfig.database,
});
if (this._mysqlConfig && typeof this._mysqlConfig.execute === "function") {
this._pool = this._mysqlConfig;
} else if (this._mysqlConfig && typeof this._mysqlConfig.promise === "function") {
this._pool = this._mysqlConfig.promise();
} else {
this._pool = getSharedPromisePool(this._mysqlConfig);
}
attachMysqlPoolGuard(this._pool, "LastFm");
await this._initTable();
return this._pool;