From ad27d6036f6182455362b973550f22a69cf727da Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Sun, 29 Nov 2026 16:02:45 +0100 Subject: [PATCH] Cap the prepared statement cache of the database pools mysql2 keeps up to 16000 prepared statements per connection by default, while MariaDB allows 16382 for the whole server. Every distinct SQL text is its own statement, and the gallery writes LIMIT and OFFSET into the text, so each page number made a new one on each of the ten connections. The OpenSpider crawl from 21 to 24 Nov filled the server's limit at 01:05 on the 24th. From then on every database client on the server failed with ER_MAX_PREPARED_STMT_COUNT_REACHED until the scheduled reboot at 05:30, including the paste and shortener cleanup jobs and stripe-reconcile. Both pools now keep at most 128 statements per connection. mysql2 closes the least recently used one when it needs room, so the shop holds at most 1920 instead of up to 240000. A new test checks the budget of both pools. --- lib/db.ts | 2 ++ lib/dbLimits.ts | 13 +++++++++++ lib/giveawayDb.ts | 2 ++ tests/dbPoolConfig.test.ts | 48 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 lib/dbLimits.ts create mode 100644 tests/dbPoolConfig.test.ts diff --git a/lib/db.ts b/lib/db.ts index 309db9d..a963d78 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -1,4 +1,5 @@ import mysql from 'mysql2/promise'; +import { MAX_PREPARED_STATEMENTS } from '@/lib/dbLimits'; // Singleton pool, reused across all API route invocations in the same process. let pool: mysql.Pool | null = null; @@ -14,6 +15,7 @@ export function getPool(): mysql.Pool { waitForConnections: true, connectionLimit: 10, queueLimit: 0, + maxPreparedStatements: MAX_PREPARED_STATEMENTS, timezone: '+00:00', }); } diff --git a/lib/dbLimits.ts b/lib/dbLimits.ts new file mode 100644 index 0000000..9b6b37c --- /dev/null +++ b/lib/dbLimits.ts @@ -0,0 +1,13 @@ +/** + * Prepared statements each pooled connection keeps open. mysql2 defaults to + * 16000, while MariaDB allows only max_prepared_stmt_count (16382) for the + * whole server, shared with every other app on it. Each distinct SQL text is + * its own statement (an inline `LIMIT n OFFSET m` makes one per page), so with + * the default a crawl over enough pages filled the server's limit and every + * client failed with ER_MAX_PREPARED_STMT_COUNT_REACHED. mysql2 closes the + * least recently used statement once this many are open. + * + * Kept apart from `lib/db.ts` so the giveaway pool can use it without counting + * as a shop-database module in tests/routeGuards.test.ts. + */ +export const MAX_PREPARED_STATEMENTS = 128; diff --git a/lib/giveawayDb.ts b/lib/giveawayDb.ts index 81b8282..0cc467a 100644 --- a/lib/giveawayDb.ts +++ b/lib/giveawayDb.ts @@ -1,4 +1,5 @@ import mysql from 'mysql2/promise'; +import { MAX_PREPARED_STATEMENTS } from '@/lib/dbLimits'; // Separate, read-only connection pool for the Giveaway-Bot database. // @@ -22,6 +23,7 @@ export function getGiveawayPool(): mysql.Pool { waitForConnections: true, connectionLimit: 5, queueLimit: 0, + maxPreparedStatements: MAX_PREPARED_STATEMENTS, timezone: '+00:00', }); } diff --git a/tests/dbPoolConfig.test.ts b/tests/dbPoolConfig.test.ts new file mode 100644 index 0000000..5e6c058 --- /dev/null +++ b/tests/dbPoolConfig.test.ts @@ -0,0 +1,48 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +// MariaDB caps prepared statements server-wide (max_prepared_stmt_count, +// 16382 here), shared by every app on the server. mysql2 keeps up to 16000 +// per connection unless told otherwise, and every distinct SQL text is a new +// statement: the gallery's `LIMIT n OFFSET m` alone made one per page. A crawl +// over the gallery pages from 21 to 24 Nov 2026 filled the server's limit and +// broke every database client until MariaDB restarted. Each pool therefore has to +// bound its cache so that pool size times cache size stays small. + +const createPool = vi.fn((config: Record) => ({ config })) + +vi.mock('mysql2/promise', () => ({ + default: { createPool }, + createPool, +})) + +// Budget for this whole process across both pools. Leaves the rest of the +// server's 16382 to the bots, the FiveM servers and the other web apps. +const PROCESS_BUDGET = 2_000 + +function poolBudget(config: Record): number { + const perConnection = Number(config.maxPreparedStatements ?? 16_000) + const connections = Number(config.connectionLimit ?? 10) + return perConnection * connections +} + +describe('database pools bound their prepared-statement cache', () => { + beforeEach(() => { + vi.resetModules() + createPool.mockClear() + }) + + it('caps the statement cache of the shop and the giveaway pool', async () => { + const { getPool } = await import('@/lib/db') + const { getGiveawayPool } = await import('@/lib/giveawayDb') + getPool() + getGiveawayPool() + + expect(createPool).toHaveBeenCalledTimes(2) + const configs = createPool.mock.calls.map(([config]) => config) + for (const config of configs) { + expect(config.maxPreparedStatements).toBeTypeOf('number') + } + const total = configs.reduce((sum, config) => sum + poolBudget(config), 0) + expect(total).toBeLessThanOrEqual(PROCESS_BUDGET) + }) +})