Counted twice, once
The code below is the file exactly as it stood in go-chi/chi. The defect was introduced on 2016-11-22, reviewed, merged, and fixed on 2026-05-16 in commit 4ef87eaf — after 3462 days in the tree.
The commit was written by a human contributor to go-chi/chi, not by an agent. The ticket and the description on the next two panels are ours — a reconstruction of the case the original change made, so you meet it the way its reviewer did. The code, the defect and the dates are untouched.
Implement io.ReaderFrom on the response wrapper
Wrapping `http.ResponseWriter` in middleware costs us `sendfile`. The standard library's writer implements `io.ReaderFrom`, and `io.Copy` uses it to hand a file straight to the kernel — but our wrapper does not implement it, so `io.Copy` falls back to a userspace buffer loop and every static file response gets slower.
Add `ReadFrom` to the fancy writer and forward to the underlying `ReaderFrom`. It has to keep working when a `Tee` writer is set, and `BytesWritten()` must stay accurate either way — the access log and the bytes-served metric both read it.
Added `ReadFrom` with two paths.
When a tee is configured we cannot delegate to the underlying `ReaderFrom`, because the kernel copy would bypass the tee entirely and the mirrored stream would be silently empty. So that branch runs `io.Copy` against the `basicWriter`, which fans out to both destinations.
With no tee we take the fast path: assert `io.ReaderFrom` on the wrapped writer, ensure the header has been written, and hand the reader over. `sendfile` is back.
Both branches add the copied byte count to `basicWriter.bytes` before returning, so `BytesWritten()` is accurate on both. Symmetrical, and easy to keep that way.
Our reconstruction of the argument the real change made, not a quotation of it. Fluent, specific, and not evidence of anything — which is the point.
func (b *basicWriter) Write(buf []byte) (n int, err error) { b.maybeWriteHeader() if !b.discard { n, err = b.ResponseWriter.Write(buf) if b.tee != nil { _, err2 := b.tee.Write(buf[:n]) // Prefer errors generated by the proxied writer. if err == nil { err = err2 } } } else if b.tee != nil { n, err = b.tee.Write(buf) } else { n, err = io.Discard.Write(buf) } b.bytes += n return n, err} func (b *basicWriter) maybeWriteHeader() { if !b.wroteHeader { b.WriteHeader(http.StatusOK) }} func (b *basicWriter) BytesWritten() int { return b.bytes} func (b *basicWriter) Tee(w io.Writer) { b.tee = w} // httpFancyWriter is a HTTP writer that additionally satisfies http.Flusher,// http.Hijacker, and io.ReaderFrom. It exists for the common case of wrapping// the http.ResponseWriter that package http gives you, in order to make the// proxied object support the full method set of the proxied object.type httpFancyWriter struct { basicWriter} func (f *httpFancyWriter) Flush() { f.wroteHeader = true fl := f.basicWriter.ResponseWriter.(http.Flusher) fl.Flush()} func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { hj := f.basicWriter.ResponseWriter.(http.Hijacker) return hj.Hijack()} func (f *httpFancyWriter) ReadFrom(r io.Reader) (int64, error) { if f.basicWriter.tee != nil { n, err := io.Copy(&f.basicWriter, r) f.basicWriter.bytes += int(n) return n, err } rf := f.basicWriter.ResponseWriter.(io.ReaderFrom) f.basicWriter.maybeWriteHeader() n, err := rf.ReadFrom(r) f.basicWriter.bytes += int(n) return n, err}