fix(transport): align WebSocket read limits with agent protocol
This commit is contained in:
1 parent
ef6e65e6f1
commit
6984a09486
11 files changed
+376
-4
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
@@ -49,6 +50,8 @@ type AgentServer struct {
|
||||
// Metrics is optional so transport tests and embeddings do not need an
|
||||
// observability endpoint. Every emitted name is a static bounded value.
|
||||
Metrics *observability.Health
|
||||
// Logf receives bounded transport diagnostics without command payloads.
|
||||
Logf func(string, ...any)
|
||||
}
|
||||
|
||||
func (server *AgentServer) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
@@ -69,6 +72,10 @@ func (server *AgentServer) ServeHTTP(response http.ResponseWriter, request *http
|
||||
http.Error(response, "agent session registry is unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if server.limits().MaxEnvelopeBytes > math.MaxInt64-1 {
|
||||
http.Error(response, "invalid agent envelope limit", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
started := time.Now()
|
||||
heartbeat := newSynchronizedHeartbeat(server.heartbeatIdle(), server.livenessTimeout(), 0)
|
||||
connection, err := websocket.Accept(response, request, &websocket.AcceptOptions{
|
||||
@@ -88,6 +95,7 @@ func (server *AgentServer) ServeHTTP(response http.ResponseWriter, request *http
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
connection.SetReadLimit(int64(server.limits().MaxEnvelopeBytes))
|
||||
server.incMetric("agent_connection")
|
||||
defer connection.CloseNow()
|
||||
server.serveConnection(request.Context(), connection, heartbeat, started)
|
||||
@@ -96,6 +104,10 @@ func (server *AgentServer) ServeHTTP(response http.ResponseWriter, request *http
|
||||
func (server *AgentServer) serveConnection(parent context.Context, connection *websocket.Conn, heartbeat *synchronizedHeartbeat, started time.Time) {
|
||||
messageType, payload, err := connection.Read(parent)
|
||||
if err != nil {
|
||||
if errors.Is(err, websocket.ErrMessageTooBig) {
|
||||
server.incMetric("agent_message_too_big")
|
||||
}
|
||||
server.logSessionError("", 0, "hello-read", err)
|
||||
return
|
||||
}
|
||||
if messageType != websocket.MessageBinary {
|
||||
@@ -143,6 +155,7 @@ func (server *AgentServer) serveConnection(parent context.Context, connection *w
|
||||
})
|
||||
server.observeMetric("sqlite_write_latency", time.Since(registrationStarted))
|
||||
if err != nil {
|
||||
server.logSessionError(hello.GetClientId(), 0, "register", err)
|
||||
server.incMetric("client_registration_failure")
|
||||
if errors.Is(err, store.ErrTakeoverRequired) {
|
||||
server.incMetric("client_takeover_required")
|
||||
@@ -242,6 +255,10 @@ func (server *AgentServer) serveConnection(parent context.Context, connection *w
|
||||
for {
|
||||
messageType, payload, err = connection.Read(sessionContext)
|
||||
if err != nil {
|
||||
if errors.Is(err, websocket.ErrMessageTooBig) {
|
||||
server.incMetric("agent_message_too_big")
|
||||
}
|
||||
server.logSessionError(hello.GetClientId(), registration.Generation, "read", err)
|
||||
return
|
||||
}
|
||||
// Elapsed time is monotonic; peer-provided wall timestamps are never used
|
||||
@@ -267,6 +284,7 @@ func (server *AgentServer) serveConnection(parent context.Context, connection *w
|
||||
if snapshot := envelope.GetReconcileSnapshot(); snapshot != nil {
|
||||
result, reconcileErr := server.Store.ReconcileClientSnapshotForSessionAt(sessionContext, hello.GetClientId(), registration.Generation, snapshot, reconcileBoundary)
|
||||
if reconcileErr != nil {
|
||||
server.logSessionError(hello.GetClientId(), registration.Generation, "reconcile", reconcileErr)
|
||||
server.close(connection, websocket.StatusPolicyViolation, "reconciliation failed")
|
||||
return
|
||||
}
|
||||
@@ -348,6 +366,7 @@ func (server *AgentServer) serveConnection(parent context.Context, connection *w
|
||||
appended, eventErr := server.Store.AppendCommandEvent(sessionContext, appendEvent)
|
||||
if eventErr != nil {
|
||||
server.incMetric("command_event_rejected")
|
||||
server.logSessionError(hello.GetClientId(), registration.Generation, "command-event", eventErr)
|
||||
server.close(connection, websocket.StatusPolicyViolation, "command event was not accepted")
|
||||
return
|
||||
}
|
||||
@@ -810,6 +829,17 @@ func (server *AgentServer) close(connection *websocket.Conn, status websocket.St
|
||||
_ = connection.Close(status, reason)
|
||||
}
|
||||
|
||||
func (server *AgentServer) logSessionError(clientID string, generation uint64, stage string, err error) {
|
||||
if server.Logf == nil || err == nil {
|
||||
return
|
||||
}
|
||||
detail := err.Error()
|
||||
if len(detail) > 1024 {
|
||||
detail = detail[:1024] + "..."
|
||||
}
|
||||
server.Logf("agent session client=%q generation=%d stage=%s error=%q", clientID, generation, stage, detail)
|
||||
}
|
||||
|
||||
func (server *AgentServer) agentPath() string {
|
||||
if server.Path == "" {
|
||||
return defaultAgentPath
|
||||
|
||||
Reference in new issue
Block a user