Spaces:
Runtime error
Runtime error
| import * as os from 'os'; | |
| // Auto-scale concurrency limit: e.g., 10 Prisma queries per CPU core | |
| const cpuCores = os.cpus().length; | |
| const concurrencyLimit = cpuCores * 10; | |
| // Dynamic import for ESM-only p-limit package | |
| let prismaConcurrencyLimit: <T>(fn: () => Promise<T>) => Promise<T>; | |
| // Initialize p-limit with dynamic import | |
| const initPLimit = (async () => { | |
| const pLimit = (await import('p-limit')).default; | |
| prismaConcurrencyLimit = pLimit(concurrencyLimit); | |
| console.log( | |
| `[safePrismaCall] Limiting Prisma concurrency to ${concurrencyLimit} (based on ${cpuCores} CPU cores)`, | |
| ); | |
| })(); | |
| export async function safePrismaCall<T = any>( | |
| fn: () => Promise<T>, | |
| ): Promise<T> { | |
| // Ensure p-limit is initialized before first use | |
| await initPLimit; | |
| return prismaConcurrencyLimit(fn); | |
| } | |