mitm, templates, context: Pool buffers to reduce allocations

Also disable some tests on context.Hostname because they're not portable
This commit is contained in:
Matthew Holt 2017-04-21 19:54:25 -06:00
parent f8614b877d
commit 0a798aafac
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
6 changed files with 53 additions and 16 deletions

View file

@ -133,7 +133,7 @@ func (c *clientHelloConn) Read(b []byte) (n int, err error) {
if err != nil {
return
}
c.buf = nil // buffer no longer needed
bufpool.Put(c.buf) // buffer no longer needed
// parse the ClientHello and store it in the map
rawParsed := parseRawClientHello(hello)
@ -285,7 +285,9 @@ func (l *tlsHelloListener) Accept() (net.Conn, error) {
if err != nil {
return nil, err
}
helloConn := &clientHelloConn{Conn: conn, listener: l, buf: new(bytes.Buffer)}
buf := bufpool.Get().(*bytes.Buffer)
buf.Reset()
helloConn := &clientHelloConn{Conn: conn, listener: l, buf: buf}
return tls.Server(helloConn, l.config), nil
}
@ -570,6 +572,13 @@ func hasGreaseCiphers(cipherSuites []uint16) bool {
return false
}
// pool buffers so we can reuse allocations over time
var bufpool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
var greaseCiphers = map[uint16]struct{}{
0x0A0A: {},
0x1A1A: {},