Avoid intermediate buffer for first len(p) bytes; import order
This commit is contained in:
parent
f49eaf293e
commit
a9c2b1fe7a
|
@ -7,11 +7,10 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
|
|
||||||
gobwas "github.com/gobwas/ws"
|
gobwas "github.com/gobwas/ws"
|
||||||
"github.com/gobwas/ws/wsutil"
|
"github.com/gobwas/ws/wsutil"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -45,16 +44,13 @@ func (c *GorillaConn) Read(p []byte) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fast path, no need to stage in the intermediate buffer
|
copied := copy(p, message)
|
||||||
if len(p) >= len(message) {
|
|
||||||
return copy(p, message), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Slow path when the entire frame won't fit in p, stage through a readBuffer so the unread bytes
|
// Write unread bytes to readBuf; if everything was read this is a no-op
|
||||||
// aren't lost
|
|
||||||
// Write returns a nil error always and grows the buffer; everything is always written or panic
|
// Write returns a nil error always and grows the buffer; everything is always written or panic
|
||||||
c.readBuf.Write(message)
|
c.readBuf.Write(message[copied:])
|
||||||
return c.readBuf.Read(p)
|
|
||||||
|
return copied, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write will write messages to the websocket connection
|
// Write will write messages to the websocket connection
|
||||||
|
|
Loading…
Reference in New Issue