feat: add thumbnail timeout and low-memory warning

This commit is contained in:
tianmo committed 2026-10-17 16:19:18 +08:00
1 parent 7b2fae6335
commit 76328fa058
10 files changed
+196 -17

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { readFileSync } from 'node:fs'
import { totalmem } from 'node:os'
export const LOW_MEMORY_THRESHOLD_BYTES = 2 * 1024 * 1024 * 1024
export function isLowMemory(bytes: number): boolean {
return bytes > 0 && bytes < LOW_MEMORY_THRESHOLD_BYTES
}
function readCgroupMemoryLimit(): number | null {
for (const path of [
'/sys/fs/cgroup/memory.max',
'/sys/fs/cgroup/memory/memory.limit_in_bytes'
]) {
try {
const raw = readFileSync(path, 'utf8').trim()
if (!raw || raw === 'max') continue
const value = Number(raw)
if (Number.isFinite(value) && value > 0) return value
} catch {
// The current platform may not use cgroup memory limits.
}
}
return null
}
export function detectSystemMemory(): {
totalBytes: number
lowMemory: boolean
thresholdBytes: number
} {
const hostMemory = totalmem()
const cgroupLimit = readCgroupMemoryLimit()
const totalBytes = cgroupLimit
? Math.min(hostMemory, cgroupLimit)
: hostMemory
return {
totalBytes,
lowMemory: isLowMemory(totalBytes),
thresholdBytes: LOW_MEMORY_THRESHOLD_BYTES
}
}