Repository navigation
perf(apiv2): keep sendfile on v2 playback and theme audio routes - #2548
Conversation
io.Copy finds io.ReaderFrom only by direct type assertion, and the v2 playback delivery and direct download writers, and the streamResponseWriter they share, had no ReadFrom. Files served through them, such as direct play, local HLS segments and original theme audio, fell back to a 32 KB user-space copy instead of the connection's sendfile path. streamResponseWriter now forwards ReadFrom once a success status is committed, as http.ServeContent does before copying. A copy that starts before any status still goes through Write, so a source that fails before its first byte still gets a problem response, and one that fails after bytes still aborts.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Problem
Related issue: N/A
Validation tasks: changes #1252 C3; changes Silo-Server/silo-apple#337 C3
Media files served through the v2 playback routes never use sendfile. Each one is copied through a 32 KB buffer in user space instead. That covers web video playback, since the web player starts every play through
/api/v2/playback/startand the plan points local media at/api/v2/stream/{session_id}, and the native clients' v2 playback as well. This change lets direct play, local HLS segments and original theme audio reach the connection'sReadFromagain.internal/httpstream/readfrom.goexplains why:io.Copyfindsio.ReaderFromonly by direct type assertion, never throughUnwrap, so one wrapper withoutReadFromturns zero-copy off for every writer below it.playbackDeliveryWriter,directDownloadWriterand thestreamResponseWriterthey share haveWrite,WriteHeader,FlushErrorandUnwrap, but noReadFrom. When direct play callshttp.ServeContent, theReadFromthatRollingDeadlineWriterand the stream telemetry writer pass down lands onplaybackDeliveryWriter, andhttpstream.ForwardReadFromfalls back toio.Copy. #1647 fixed the same gap instatusRecorderand the managed download routes; these writers were missed.Both validation cases above passed direct play, remux and HLS in rehearsal runs. The change affects how bytes are copied, not which bytes or headers are sent, so they should still pass.
Approach
streamResponseWriter.ReadFromforwards throughhttpstream.ForwardReadFromonce a success status is committed.http.ServeContentwrites its status before it copies, so every file it serves on these routes takes the fast path.Write, which commits 200 only on the first byte. A source that fails before yielding anything still gets its v2 problem response, and one that fails after bytes still ends the stream instead of appending JSON. No handler on these routes reachesReadFrombefore setting a status today; this keeps the writer's rule the same whichever way bytes arrive. After a rejected status, copied bytes are still discarded.playbackDeliveryWriteranddirectDownloadWriterhandReadFromto that writer, as they already do forWrite.directDownloadWriterserves the direct download and theme song routes.Still copied in user space after this change:
/api/v2/direct-download. The download service wraps the file and the writer to catch read errors and short transfers (fix(downloads): keep interrupted direct downloads from appending a JSON error #2235), so sendfile can't see the file whatever the v2 writer does. Fixing that is a separate change ininternal/downloads.statusRecorderthere and have noReadFrom. A subtitle cache hit now reaches those wrappers instead of stopping at the v2 writer. These bodies are small text.jellycompat and
/api/v1don't use these writers.Validation
Tested commit: f6436f4, on
mainat 9a2b950.internal/apiv2tests pass, exceptTestAdminResourceCapabilitiesAndScope, which needs Linux cgroups and fails the same way onmainon macOS.go vetandgofmtare clean on the package.themesongs.Serve. They check that every byte reaches the connection'sReadFromfrom the*os.File, under at most oneio.LimitedReader, which is what sendfile needs. Onmainno byte does, and both tests fail.maintoo and guard the new path.docs/architecture/streaming-write-deadline.mdnames as the conformance check, and I didn't trace sendfile calls withstrace.make lint-changed,make test-goand the contract checks. My machine blocks whole-repo runs; CI runs them. No web files changed.Benchmarks
A 256 MiB file served on the v2 stream route by
playback.ServeDirectPlay, throughapiv2.NewHandlerwith its middleware, over loopback TCP to a client in the same process.mainat 9a2b950 against this branch, five runs oftesting.Benchmarkeach, macOS on Apple silicon, Go 1.27.1. Each cell is the lowest to highest of the five runs.mainThe allocation count rises by about five for each 4 MiB slice
RollingDeadlineWriterhands down; the bytes allocated still halve.Limits:
Benchmark source (a throwaway test in
internal/apiv2, not committed)Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-2548/
Nothing visible changes: responses keep the same bytes and headers. The page has the test output on
mainand on this branch (no file bytes reachReadFromonmain, all of them do here) and the raw benchmark runs.Risks
None identified. A copy that fails partway through a committed response now fails inside the wrapped writer's
ReadFrominstead ofWrite, and the stream ends the same way.Checklist
AI Disclosure
ReadFrom, and narrowed this description. It also asked for a clearer code comment, which is in.Note
Keep zero-copy
sendfileon v2 playback and theme audio routesAdds
ReadFrommethods soio.Copycan use the underlying transport's reader-from path when serving file-backed media.directDownloadWriter.ReadFromandplaybackDeliveryWriter.ReadFromin direct_download.go and playback_delivery.go delegate copying to the wrapped transport.streamResponseWriter.ReadFromin stream_response_writer.go routes pre-status and rejected copies through the write-only adapter, while committed responses forward to the wrapped writer'sReadFrom, preserving first-byte commitment and failure handling.Macroscope summarized f6436f4.