|
|
@@ -265,6 +265,10 @@ type Conn struct {
|
|
|
}
|
|
|
|
|
|
func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int) *Conn {
|
|
|
+ return newConnBRW(conn, isServer, readBufferSize, writeBufferSize, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func newConnBRW(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int, brw *bufio.ReadWriter) *Conn {
|
|
|
mu := make(chan bool, 1)
|
|
|
mu <- true
|
|
|
|
|
|
@@ -274,13 +278,28 @@ func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int)
|
|
|
if readBufferSize < maxControlFramePayloadSize {
|
|
|
readBufferSize = maxControlFramePayloadSize
|
|
|
}
|
|
|
+
|
|
|
+ // Reuse the supplied brw.Reader if brw.Reader's buf is the requested size.
|
|
|
+ var br *bufio.Reader
|
|
|
+ if brw != nil && brw.Reader != nil {
|
|
|
+ // This code assumes that peek on a reset reader returns
|
|
|
+ // bufio.Reader.buf[:0].
|
|
|
+ brw.Reader.Reset(conn)
|
|
|
+ if p, err := brw.Reader.Peek(0); err == nil && cap(p) == readBufferSize {
|
|
|
+ br = brw.Reader
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if br == nil {
|
|
|
+ br = bufio.NewReaderSize(conn, readBufferSize)
|
|
|
+ }
|
|
|
+
|
|
|
if writeBufferSize == 0 {
|
|
|
writeBufferSize = defaultWriteBufferSize
|
|
|
}
|
|
|
|
|
|
c := &Conn{
|
|
|
isServer: isServer,
|
|
|
- br: bufio.NewReaderSize(conn, readBufferSize),
|
|
|
+ br: br,
|
|
|
conn: conn,
|
|
|
mu: mu,
|
|
|
readFinal: true,
|