Replace bytes.Buffer with vanilla []byte in tls wrapper

This commit is contained in:
Andy Wang 2020-12-24 10:26:53 +00:00
parent 4209483a48
commit 5933ad8781
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"io" "io"
"net" "net"
@ -44,7 +43,9 @@ func NewTLSConn(conn net.Conn) *TLSConn {
return &TLSConn{ return &TLSConn{
Conn: conn, Conn: conn,
writeBufPool: sync.Pool{New: func() interface{} { writeBufPool: sync.Pool{New: func() interface{} {
return new(bytes.Buffer) b := make([]byte, 0, initialWriteBufSize)
b = append(b, ApplicationData, byte(VersionTLS13>>8), byte(VersionTLS13&0xFF))
return &b
}}, }},
} }
} }
@ -93,16 +94,13 @@ func (tls *TLSConn) Read(buffer []byte) (n int, err error) {
func (tls *TLSConn) Write(in []byte) (n int, err error) { func (tls *TLSConn) Write(in []byte) (n int, err error) {
msgLen := len(in) msgLen := len(in)
writeBuf := tls.writeBufPool.Get().(*bytes.Buffer) writeBuf := tls.writeBufPool.Get().(*[]byte)
writeBuf.WriteByte(ApplicationData) *writeBuf = append(*writeBuf, byte(msgLen>>8), byte(msgLen&0xFF))
writeBuf.WriteByte(byte(VersionTLS13 >> 8)) *writeBuf = append(*writeBuf, in...)
writeBuf.WriteByte(byte(VersionTLS13 & 0xFF)) n, err = tls.Conn.Write(*writeBuf)
writeBuf.WriteByte(byte(msgLen >> 8)) *writeBuf = (*writeBuf)[:3]
writeBuf.WriteByte(byte(msgLen & 0xFF))
writeBuf.Write(in)
i, err := writeBuf.WriteTo(tls.Conn)
tls.writeBufPool.Put(writeBuf) tls.writeBufPool.Put(writeBuf)
return int(i - recordLayerLength), err return n - recordLayerLength, err
} }
func (tls *TLSConn) Close() error { func (tls *TLSConn) Close() error {