fix(artwork): convert animated GIF covers in the background

Resized animated GIF covers were converted to animated WebP by ffmpeg inside
the image request, bound to its 10s timeout. On slow CPUs large GIFs never
finished: ffmpeg was killed, nothing was cached, and every request repeated
the work. With ffmpeg 5.1 the conversion also failed mid-stream when reading
the GIF from a pipe, after the stream was already handed to the client, so
there was no fallback.

Requests now get a static stand-in right away, served with no-store and no
validators, while the conversion runs in the background. A single
process-wide slot bounds these conversions, since they outlive the request
throttle; when it is busy nothing is queued and a later request retries.
The result, or a static fallback if ffmpeg fails, is cached under the same
key. ffmpeg output is now buffered so mid-stream failures fall back to a
static resize. The file cache gains cache.Transient for loader results that
must be served but not stored. Without an available cache, conversion stays
inline.
This commit is contained in:
Deluan 2026-09-14 19:36:50 -04:00
commit bc16cafb91
9 changed files with 362 additions and 9 deletions

View file

@ -13,8 +13,8 @@ import (
// (the caller must then not write a body). requestedHash is the hash the client asserted, or "".
func WriteImageHeaders(w http.ResponseWriter, r *http.Request, img *artwork.Image, requestedHash string) (wrote304 bool) {
h := w.Header()
// Placeholders are transient stand-ins for unresolved art: never cached, no validators.
if img.Placeholder {
// Placeholders and transient stand-ins must not outlive what they stand in for: never cached, no validators.
if img.Placeholder || img.Transient {
h.Set("Cache-Control", "no-store")
return false
}

View file

@ -41,6 +41,12 @@ func resized() *artwork.Image {
}
}
func transient() *artwork.Image {
img := resized()
img.Transient = true
return img
}
func unvalidated() *artwork.Image {
return &artwork.Image{ReadCloser: io.NopCloser(strings.NewReader("IMG")), LastUpdated: lastMod}
}
@ -105,6 +111,9 @@ var _ = Describe("WriteImageHeaders", func() {
testCase{img: found(), ifNoneMatch: `"deadbeefdeadbeef"`, want304: false, wantCache: "public, no-cache", wantETag: `"` + testHash + `"`, wantLastMod: true}),
Entry("placeholder ignores If-None-Match and never 304s",
testCase{img: placeholder(), ifNoneMatch: "*", want304: false, wantCache: "no-store"}),
// A stand-in shares the final representation's ETag inputs, so a validator would pin it.
Entry("transient stand-in is never cached and carries no validators",
testCase{img: transient(), ifNoneMatch: `"` + testRepTag + `"`, want304: false, wantCache: "no-store"}),
// With no validator, an emitted ETag would be the same empty tag on every such response,
// and matching it would 304 changed bytes.