Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/db.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +15,7 @@ export function getPool(): mysql.Pool {
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
maxPreparedStatements: MAX_PREPARED_STATEMENTS,
timezone: '+00:00',
});
}
Expand Down
13 changes: 13 additions & 0 deletions lib/dbLimits.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions lib/giveawayDb.ts
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -22,6 +23,7 @@ export function getGiveawayPool(): mysql.Pool {
waitForConnections: true,
connectionLimit: 5,
queueLimit: 0,
maxPreparedStatements: MAX_PREPARED_STATEMENTS,
timezone: '+00:00',
});
}
Expand Down
48 changes: 48 additions & 0 deletions tests/dbPoolConfig.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) => ({ 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<string, unknown>): 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)
})
})