Исправьте кадрирование и очистку рабочих файлов кодека

This commit is contained in:
2026-11-20 10:54:34 +03:00
parent 7898074f19
commit 42a9bf2071
5 changed files with 76 additions and 10 deletions
+20 -2
View File
@@ -36,6 +36,24 @@ var ErrInvalidImage = errors.New("invalid image or pipeline version")
// occupy hundreds of MiB. A worker with a 2 GiB cgroup can run two ordinary jobs.
var decoderSlots = semaphore.NewWeighted(2)
func scratchRoot() string {
return filepath.Join(os.TempDir(), "messenger-worker-imaging")
}
// CleanScratch reclaims pixel caches left by a killed worker. The consumer
// calls it before starting any jobs in its container's private filesystem.
func CleanScratch() error {
return os.RemoveAll(scratchRoot())
}
func newRenderDir() (string, error) {
root := scratchRoot()
if err := os.MkdirAll(root, 0700); err != nil {
return "", err
}
return os.MkdirTemp(root, "attachment-")
}
// Render decodes once in a deadline-bound child process. Its pixel cache can
// spill to a private disk directory when a high-resolution source exceeds RAM.
func Render(ctx context.Context, source []byte, version int) ([]Variant, error) {
@@ -61,7 +79,7 @@ func Render(ctx context.Context, source []byte, version int) ([]Variant, error)
return nil, err
}
defer decoderSlots.Release(weight)
dir, err := os.MkdirTemp("", "attachment-")
dir, err := newRenderDir()
if err != nil {
return nil, err
}
@@ -93,7 +111,7 @@ func Render(ctx context.Context, source []byte, version int) ([]Variant, error)
args := []string{
"-limit", "thread", "1", "-limit", "memory", "96MiB",
"-limit", "map", "0", "-limit", "disk", "2GiB",
input, "-colorspace", "sRGB",
input, "+repage", "-colorspace", "sRGB",
}
for _, v := range output {
args = append(args, "(")