mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 12:50:09 +00:00
3820b9b21d
Add connection pool params to DATABASE_URL and configure Prisma to use the pooled URL (connection_limit=20, pool_timeout=30) to reduce connection exhaustion. Introduce safeguards and throttling across processors: limit in-flight progress DB updates in direct-download, add short delays when processing RSS, retry-failed-imports, and retry-missing-torrents, and stagger scheduler triggers to avoid bursts. Implement adaptive monitor-download polling with stallCount/lastProgress and exponential backoff, and thread these fields through JobQueueService (including reduced worker concurrency for several queues). Batch audiobook enrichment queries to small parallel batches to limit DB load. Update tests to reflect new monitor payload parameters. Overall intent: reduce DB connection pool pressure and smooth load spikes during startup and heavy processing.
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* Component: Database Client
|
|
* Documentation: documentation/backend/database.md
|
|
*/
|
|
|
|
import { PrismaClient } from '@/generated/prisma/client';
|
|
|
|
/**
|
|
* Append connection pool parameters to DATABASE_URL if not already present.
|
|
* - connection_limit=20: up from default 9, fits 22 max workers + API routes
|
|
* - pool_timeout=30: up from default 10s, gives queued requests time
|
|
*/
|
|
function getPooledDatabaseUrl(): string {
|
|
const baseUrl = process.env.DATABASE_URL || '';
|
|
if (!baseUrl) return baseUrl;
|
|
|
|
const separator = baseUrl.includes('?') ? '&' : '?';
|
|
const params: string[] = [];
|
|
|
|
if (!baseUrl.includes('connection_limit')) {
|
|
params.push('connection_limit=20');
|
|
}
|
|
if (!baseUrl.includes('pool_timeout')) {
|
|
params.push('pool_timeout=30');
|
|
}
|
|
|
|
if (params.length === 0) return baseUrl;
|
|
return `${baseUrl}${separator}${params.join('&')}`;
|
|
}
|
|
|
|
// Prevent multiple instances of Prisma Client in development
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
export const prisma =
|
|
globalForPrisma.prisma ??
|
|
new PrismaClient({
|
|
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
|
datasources: {
|
|
db: {
|
|
url: getPooledDatabaseUrl(),
|
|
},
|
|
},
|
|
});
|
|
|
|
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
|
|
|
|
// Graceful shutdown
|
|
process.on('beforeExit', async () => {
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
export default prisma;
|