Skip to content

Commit 25b0691

Browse files
committed
http: reuse the connection write buffer when hijacking
Previously, the connection write buffer used an extra 4kB of memory until the handler finished and the "conn" was garbage collected. ``` goos: linux goarch: amd64 pkg: net/http cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ ServerHijack-8 13.28µ ± 2% 10.84µ ± 3% -18.36% (p=0.000 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ ServerHijack-8 15.84Ki ± 0% 11.77Ki ± 0% -25.70% (p=0.000 n=10) │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ ServerHijack-8 50.00 ± 0% 48.00 ± 0% -4.00% (p=0.000 n=10) ``` Signed-off-by: Jakob Ackermann <[email protected]>
1 parent 35222ee commit 25b0691

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/net/http/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
322322
rwc = c.rwc
323323
rwc.SetDeadline(time.Time{})
324324

325-
buf = bufio.NewReadWriter(c.bufr, bufio.NewWriter(rwc))
325+
buf = bufio.NewReadWriter(c.bufr, c.bufw)
326326
if c.r.hasByte {
327327
if _, err := c.bufr.Peek(c.bufr.Buffered() + 1); err != nil {
328328
return nil, nil, fmt.Errorf("unexpected Peek failure reading buffered byte: %v", err)

0 commit comments

Comments
 (0)