fix(grid): bound WebSocket read reservations and update gobwas/ws

Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
Feng Ruohang
2026-11-25 11:46:51 +08:00
parent d66020003c
commit e5296263f0
6 changed files with 29 additions and 37 deletions
+2 -5
View File
@@ -965,7 +965,6 @@ func (c *Connection) readStream(ctx context.Context, conn net.Conn, cancel conte
OnIntermediate: controlHandler,
}
readDataInto := func(dst []byte, s ws.State, want ws.OpCode) ([]byte, error) {
dst = dst[:0]
for {
hdr, err := wsReader.NextFrame()
if err != nil {
@@ -983,13 +982,11 @@ func (c *Connection) readStream(ctx context.Context, conn net.Conn, cancel conte
}
continue
}
if int64(cap(dst)) < hdr.Length+1 {
dst = make([]byte, 0, hdr.Length+hdr.Length>>3)
}
dst = frameReadBuffer(dst, hdr.Length)
if !hdr.Fin {
hdr.Length = -1
}
return readAllInto(dst[:0], &wsReader, hdr.Length)
return readAllInto(dst, &wsReader, hdr.Length)
}
}
+10
View File
@@ -122,6 +122,16 @@ var PutByteBuffer = func(b []byte) {
}
}
// frameReadBuffer reserves at most one read buffer from the frame header.
// readAllInto grows it as the payload arrives.
func frameReadBuffer(b []byte, length int64) []byte {
initial := min(length, int64(readBufferSize))
if int64(cap(b)) >= initial {
return b[:0]
}
return make([]byte, 0, initial)
}
// readAllInto reads from r and appends to b until an error or EOF and returns the data it read.
// A successful call returns err == nil, not err == EOF. Because readAllInto is
// defined to read from src until EOF, it does not treat an EOF from Read
+14
View File
@@ -34,6 +34,20 @@ import (
"github.com/minio/minio/internal/logger/target/testlogger"
)
func TestFrameReadBuffer(t *testing.T) {
for _, length := range []int64{0, 128, readBufferSize, 4 * readBufferSize} {
buf := frameReadBuffer(nil, length)
if len(buf) != 0 || int64(cap(buf)) != min(length, int64(readBufferSize)) {
t.Fatalf("length %d: got len %d, cap %d", length, len(buf), cap(buf))
}
}
buf := make([]byte, 128)
got := frameReadBuffer(buf, int64(len(buf)))
if len(got) != 0 || cap(got) != cap(buf) || &got[:1][0] != &buf[0] {
t.Fatal("did not reuse the existing read buffer")
}
}
func TestSingleRoundtrip(t *testing.T) {
defer testlogger.T.SetLogTB(t)()
errFatal := func(err error) {