Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
fix(memory): prevent server OOM from unbounded Next.js fetch cache and dataset info cache
Browse files- Add `cache: "no-store"` to fetchParquetFile, fetchJson, and tasks.jsonl fetch
so Next.js does not accumulate large parquet ArrayBuffers in its Data Cache
across many requests over days of continuous operation
- Cap datasetInfoCache at 200 entries with FIFO eviction to prevent unbounded growth
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Files changed (3)
362 unmodified lines363364365366367368369362 unmodified linesif (!task && allData.length > 0) {try {const tasksUrl = buildVersionedUrl(repoId, version, "meta/tasks.jsonl");const tasksResponse = await fetch(tasksUrl);if (tasksResponse.ok) {const tasksText = await tasksResponse.text();362 unmodified lines363364365366367368369362 unmodified linesif (!task && allData.length > 0) {try {const tasksUrl = buildVersionedUrl(repoId, version, "meta/tasks.jsonl");const tasksResponse = await fetch(tasksUrl, { cache: "no-store" });if (tasksResponse.ok) {const tasksText = await tasksResponse.text();
24 unmodified lines2526272829303111 unmodified lines4344454647484924 unmodified lines}export async function fetchJson<T>(url: string): Promise<T> {const res = await fetch(url);if (!res.ok) {throw new Error(`Failed to fetch JSON ${url}: ${res.status} ${res.statusText}`,11 unmodified lines// Fetch and parse the Parquet fileexport async function fetchParquetFile(url: string): Promise<ArrayBuffer> {const res = await fetch(url);if (!res.ok) {throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);24 unmodified lines2526272829303111 unmodified lines4344454647484924 unmodified lines}export async function fetchJson<T>(url: string): Promise<T> {const res = await fetch(url, { cache: "no-store" });if (!res.ok) {throw new Error(`Failed to fetch JSON ${url}: ${res.status} ${res.statusText}`,11 unmodified lines// Fetch and parse the Parquet fileexport async function fetchParquetFile(url: string): Promise<ArrayBuffer> {const res = await fetch(url, { cache: "no-store" });if (!res.ok) {throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
30 unmodified lines31323334353637383940414229 unmodified lines72737475767730 unmodified linesfeatures: Record<string, FeatureInfo>;}// In-memory cache for dataset info (5 min TTL)const datasetInfoCache = new Map<string,{ data: DatasetInfo; expiry: number }>();const CACHE_TTL_MS = 5 * 60 * 1000;export async function getDatasetInfo(repoId: string): Promise<DatasetInfo> {const cached = datasetInfoCache.get(repoId);29 unmodified lines);}datasetInfoCache.set(repoId, {data: data as DatasetInfo,expiry: Date.now() + CACHE_TTL_MS,30 unmodified lines3132333435363738394041424329 unmodified lines7374757677787980818230 unmodified linesfeatures: Record<string, FeatureInfo>;}// In-memory cache for dataset info (5 min TTL, max 200 entries)const datasetInfoCache = new Map<string,{ data: DatasetInfo; expiry: number }>();const CACHE_TTL_MS = 5 * 60 * 1000;const CACHE_MAX_SIZE = 200;export async function getDatasetInfo(repoId: string): Promise<DatasetInfo> {const cached = datasetInfoCache.get(repoId);29 unmodified lines);}if (datasetInfoCache.size >= CACHE_MAX_SIZE) {// Evict the oldest entry (Maps preserve insertion order)datasetInfoCache.delete(datasetInfoCache.keys().next().value!);}datasetInfoCache.set(repoId, {data: data as DatasetInfo,expiry: Date.now() + CACHE_TTL_MS,
src/app/[org]/[dataset]/[episode]/fetch-data.ts+1-1
src/utils/parquetUtils.ts+2-2
src/utils/versionUtils.ts+6-1