Static file server reads whole files into memory: no streaming, no Range, no conditional requests #36
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
serve_from_dir(proxy.rs:1037) reads each file fully into aVec<u8>before responding:So peak memory is file size × concurrent requests. A few-hundred-MB asset served to several clients at once is multiple GB of RSS, and a
HEADcosts the same read even though hyper discards the body.Also absent, all consequences of the same shape:
Rangesupport — a client that asks for a byte range gets the whole file with200, and a resumed download restarts.ETag/Last-Modified, so no conditional requests: every fetch is a full transfer and re-read.Content-Lengthbefore the read — it is only known once the file is buffered.The fix is to stream: open the file and wrap it in a
tokio_util::io::ReaderStreamas the body, withContent-Lengthfrom the metadata, and handleRangefor large assets.tower-http'sServeDirdoes all of this and is already an indirect dependency, though it would need the access-control and fallback semantics inserve_staticlayered around it rather than replacing them.This is fine for the small HTML/CSS/image sites the static server was written for, and a blocker for anything large. Found while assessing ER for serving netboot images (an initrd of a few hundred MB); not fixed inline per the standing rule.