fix(transport): align WebSocket read limits with agent protocol

This commit is contained in:
cabbage committed 2026-11-09 07:53:18 +00:00
1 parent ef6e65e6f1
commit 6984a09486
11 files changed
+376 -4

No files matched your search

+18
View File
@@ -3,9 +3,12 @@ package agent
import (
"context"
"errors"
"fmt"
"math"
"net/http"
"github.com/coder/websocket"
"github.com/rvbox/rvbox/internal/agentproto"
)
var ErrUnexpectedMessage = errors.New("agent transport received a non-binary WebSocket message")
@@ -13,10 +16,25 @@ var ErrUnexpectedMessage = errors.New("agent transport received a non-binary Web
type WebSocketTransport struct{ connection *websocket.Conn }
func DialWebSocket(ctx context.Context, address string, client *http.Client) (*WebSocketTransport, error) {
return DialWebSocketWithLimits(ctx, address, client, agentproto.DefaultLimits())
}
// DialWebSocketWithLimits applies the same message ceiling as envelope
// validation. The library's 32 KiB default is smaller than ordinary RVBox
// chunks, dispatches, and reconciliation history.
func DialWebSocketWithLimits(ctx context.Context, address string, client *http.Client, limits agentproto.Limits) (*WebSocketTransport, error) {
readLimit := limits.MaxEnvelopeBytes
if readLimit == 0 {
readLimit = agentproto.DefaultLimits().MaxEnvelopeBytes
}
if readLimit > math.MaxInt64-1 {
return nil, fmt.Errorf("invalid WebSocket envelope limit: %d", readLimit)
}
connection, _, err := websocket.Dial(ctx, address, &websocket.DialOptions{HTTPClient: client, CompressionMode: websocket.CompressionDisabled})
if err != nil {
return nil, err
}
connection.SetReadLimit(int64(readLimit))
return &WebSocketTransport{connection: connection}, nil
}