fix(operations): preserve response processing deadlines and await routed API readiness

This commit is contained in:
2026-11-22 20:04:24 +03:00
parent f77223790d
commit 7e61bc4ee3
5 changed files with 266 additions and 3 deletions
+6 -1
View File
@@ -365,6 +365,7 @@ type drainingRequestBody struct {
mu sync.Mutex
once sync.Once
deadline func()
reset func()
}
func (b *drainingRequestBody) Read(data []byte) (int, error) {
@@ -376,6 +377,7 @@ func (b *drainingRequestBody) Read(data []byte) (int, error) {
func (b *drainingRequestBody) Close() error {
b.once.Do(func() {
b.deadline()
defer b.reset()
b.mu.Lock()
defer b.mu.Unlock()
// Drain within a short deadline after an early response so closing TLS does
@@ -390,9 +392,12 @@ func serveLoopbackProxy(c Config, proxy *httputil.ReverseProxy, w http.ResponseW
if c.Role == "api" && r.Body != nil && r.Body != http.NoBody {
controller := http.NewResponseController(w)
_ = controller.EnableFullDuplex()
_ = controller.SetReadDeadline(time.Now().Add(25 * time.Second))
receiveDeadline := time.Now().Add(25 * time.Second)
_ = controller.SetReadDeadline(receiveDeadline)
body := &drainingRequestBody{ReadCloser: r.Body, deadline: func() {
_ = controller.SetReadDeadline(time.Now().Add(2 * time.Second))
}, reset: func() {
_ = controller.SetReadDeadline(receiveDeadline)
}}
r.Body = body
defer body.Close()
+7 -1
View File
@@ -51,6 +51,8 @@ func TestEarlyRejectionDoesNotForwardBodyAndAcceptedBodyStillStreams(t *testing.
if err != nil {
t.Error(err)
}
// Request-body cleanup must not cancel legitimate processing.
time.Sleep(2200 * time.Millisecond)
w.Write(data)
}))
defer backend.Close()
@@ -75,7 +77,11 @@ func TestEarlyRejectionDoesNotForwardBodyAndAcceptedBodyStillStreams(t *testing.
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(5 * time.Second))
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: synthetic\r\nConnection: close\r\nContent-Type: image/png\r\nContent-Length: %d\r\n\r\n", len(payload))
connection := "keep-alive"
if reject {
connection = "close"
}
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: synthetic\r\nConnection: %s\r\nContent-Type: image/png\r\nContent-Length: %d\r\n\r\n", connection, len(payload))
// A load balancer can finish writing before reading the early reply.
if _, err := io.WriteString(conn, payload); err != nil {
t.Fatal(err)