Skip to content

Commit

Permalink
http: reuse the connection write buffer when hijacking
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
das7pad committed Dec 19, 2023
1 parent 35222ee commit 25b0691
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (c *conn) hijackLocked() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
rwc = c.rwc
rwc.SetDeadline(time.Time{})

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

0 comments on commit 25b0691

Please sign in to comment.