Skip to content

Commit

Permalink
crypto/tls: shrink tls Conn's rawInput buffer when Read timeout
Browse files Browse the repository at this point in the history
This will reduce the rawInput buffer memory cost for large number of idle tls connections
  • Loading branch information
cch123 committed Sep 8, 2021
1 parent 6226020 commit 51de0f8
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/crypto/tls/conn.go
Expand Up @@ -797,6 +797,14 @@ func (c *Conn) readFromUntil(r io.Reader, n int) error {
// "predict" closeNotify alerts.
c.rawInput.Grow(needs + bytes.MinRead)
_, err := c.rawInput.ReadFrom(&atLeastReader{r, int64(needs)})

// Read timeout, and we do not get any data, connection is idle,
// we should replace rawInput buffer with a small one
if e, ok := err.(net.Error); ok && e.Timeout() &&
c.rawInput.Len() == 0 && c.rawInput.Cap() > 4*bytes.MinRead {
c.rawInput = *bytes.NewBuffer(make([]byte, 0, bytes.MinRead))
}

return err
}

Expand Down

0 comments on commit 51de0f8

Please sign in to comment.