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) + }) +})