diff --git a/server/src/index.ts b/server/src/index.ts index c01a72a..54c5ec7 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -162,19 +162,20 @@ fs.mkdirSync(NORM_CACHE_DIR, { recursive: true }); // Danach kein Disk-I/O mehr bei Cache-Hits -> nahezu instant playback. const pcmMemoryCache = new Map(); const PCM_MEMORY_CACHE_MAX_MB = Number(process.env.PCM_CACHE_MAX_MB ?? '512'); +const PCM_PER_FILE_MAX_MB = 50; // Max 50 MB pro Datei (~4.5 min PCM) - groessere werden gestreamt let pcmMemoryCacheBytes = 0; function getPcmFromMemory(cachedPath: string): Buffer | null { const buf = pcmMemoryCache.get(cachedPath); if (buf) return buf; - // Erste Anfrage: von Disk in RAM laden try { + // Dateigroesse VORHER pruefen - keine grossen Files in den RAM laden + const stat = fs.statSync(cachedPath); + if (stat.size > PCM_PER_FILE_MAX_MB * 1024 * 1024) return null; + if (pcmMemoryCacheBytes + stat.size > PCM_MEMORY_CACHE_MAX_MB * 1024 * 1024) return null; const data = fs.readFileSync(cachedPath); - const newTotal = pcmMemoryCacheBytes + data.byteLength; - if (newTotal <= PCM_MEMORY_CACHE_MAX_MB * 1024 * 1024) { - pcmMemoryCache.set(cachedPath, data); - pcmMemoryCacheBytes = newTotal; - } + pcmMemoryCache.set(cachedPath, data); + pcmMemoryCacheBytes += data.byteLength; return data; } catch { return null; } }