feat: limit memory usage for previewing large images (#2602)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
ze0987 2025-04-11 07:03:45 +02:00 committed by GitHub
parent 7cc250180f
commit ca4fe2ced7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View file

@ -84,7 +84,7 @@ micro_workers = 10
macro_workers = 10
bizarre_retry = 3
image_alloc = 536870912 # 512MB
image_bound = [ 0, 0 ]
image_bound = [ 5000, 5000 ]
suppress_preload = false
[plugin]

View file

@ -24,7 +24,7 @@ function M:preload(job)
return true
end
local cmd = M.with_env()
local cmd = M.with_limit()
if job.args.flatten then
cmd = cmd:arg("-flatten")
end
@ -46,10 +46,16 @@ end
function M:spot(job) require("file"):spot(job) end
function M.with_env()
local cmd = Command("magick"):env("MAGICK_THREAD_LIMIT", 1)
function M.with_limit()
local cmd = Command("magick"):args { "-limit", "thread", 1 }
if rt.tasks.image_alloc > 0 then
cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc)
cmd = cmd:args({ "-limit", "memory", rt.tasks.image_alloc }):args { "-limit", "disk", "1MiB" }
end
if rt.tasks.image_bound[1] > 0 then
cmd = cmd:args { "-limit", "width", rt.tasks.image_bound[1] }
end
if rt.tasks.image_bound[2] > 0 then
cmd = cmd:args { "-limit", "height", rt.tasks.image_bound[2] }
end
return cmd
end