mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(resp): flag pipeline failures that never reached the wire
A pipeline that failed before it wrote its request cannot have changed the server. The error now says so, and respNotApplied reads that flag. It walks Unwrap by hand because yaegi has no errors.As.
This commit is contained in:
@@ -318,7 +318,7 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any
|
|||||||
|
|
||||||
pc, err := c.acquire(deadline)
|
pc, err := c.acquire(deadline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, ¬AppliedError{err: err}
|
||||||
}
|
}
|
||||||
defer func() { c.free <- pc }()
|
defer func() { c.free <- pc }()
|
||||||
|
|
||||||
@@ -328,20 +328,50 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any
|
|||||||
}
|
}
|
||||||
closeConn(pc)
|
closeConn(pc)
|
||||||
if isTimeout(err) {
|
if isTimeout(err) {
|
||||||
return nil, err
|
return nil, markNotApplied(err, wrote)
|
||||||
}
|
}
|
||||||
if wrote && readAny && mutates {
|
if wrote && readAny && mutates {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
replies, _, _, err = c.attemptEncodedOn(pc, payload, n, deadline)
|
replies, wrote, _, err = c.attemptEncodedOn(pc, payload, n, deadline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
closeConn(pc)
|
closeConn(pc)
|
||||||
return nil, err
|
return nil, markNotApplied(err, wrote)
|
||||||
}
|
}
|
||||||
return replies, nil
|
return replies, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notAppliedError marks a pipeline failure that happened before the request reached the wire,
|
||||||
|
// so the server cannot have applied any command in it.
|
||||||
|
type notAppliedError struct{ err error }
|
||||||
|
|
||||||
|
func (e *notAppliedError) Error() string { return e.err.Error() }
|
||||||
|
func (e *notAppliedError) Unwrap() error { return e.err }
|
||||||
|
|
||||||
|
func markNotApplied(err error, wrote bool) error {
|
||||||
|
if wrote {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ¬AppliedError{err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
// respNotApplied reports whether err says the failed call never wrote its request.
|
||||||
|
// It walks Unwrap by hand because errors.As is unsupported under yaegi.
|
||||||
|
func respNotApplied(err error) bool {
|
||||||
|
for err != nil {
|
||||||
|
if _, ok := err.(*notAppliedError); ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
u, ok := err.(interface{ Unwrap() error })
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
err = u.Unwrap()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// pipelineMutatingCommands names every RESP command this client ever sends
|
// pipelineMutatingCommands names every RESP command this client ever sends
|
||||||
// that is NOT safe to blindly resend: each already changes server state on
|
// that is NOT safe to blindly resend: each already changes server state on
|
||||||
// success, so replaying it after an ambiguous failure (the request bytes
|
// success, so replaying it after an ambiguous failure (the request bytes
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package traefikllmgateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRESPPipelineNotApplied(t *testing.T) {
|
||||||
|
incr := [][]string{{"INCRBY", "k", "1"}, {"EXPIRE", "k", "60"}}
|
||||||
|
|
||||||
|
t.Run("dial failure was never written", func(t *testing.T) {
|
||||||
|
ln := newFakeListener(t)
|
||||||
|
addr := ln.Addr().String()
|
||||||
|
_ = ln.Close()
|
||||||
|
c := newRESPClient(addr, "", 0)
|
||||||
|
_, err := c.pipeline(incr)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("want an error dialing a dead address")
|
||||||
|
}
|
||||||
|
if !respNotApplied(err) {
|
||||||
|
t.Errorf("respNotApplied(%v) = false, want true", err)
|
||||||
|
}
|
||||||
|
if !respNotApplied(fmt.Errorf("wrapped: %w", err)) {
|
||||||
|
t.Error("respNotApplied lost the marker through fmt.Errorf %w")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("reply lost after bytes arrived is ambiguous", func(t *testing.T) {
|
||||||
|
ln := newFakeListener(t)
|
||||||
|
go func() {
|
||||||
|
conn, err := ln.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = ln.Close()
|
||||||
|
defer func() { _ = conn.Close() }()
|
||||||
|
r := bufio.NewReader(conn)
|
||||||
|
_, _ = readRESPCommand(r) // SELECT
|
||||||
|
_, _ = conn.Write([]byte("+OK\r\n"))
|
||||||
|
_, _ = readRESPCommand(r)
|
||||||
|
_, _ = readRESPCommand(r)
|
||||||
|
_, _ = conn.Write([]byte(":1\r\n"))
|
||||||
|
}()
|
||||||
|
c := newRESPClient(ln.Addr().String(), "", 0)
|
||||||
|
_, err := c.pipeline(incr)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("want an error")
|
||||||
|
}
|
||||||
|
if respNotApplied(err) {
|
||||||
|
t.Errorf("respNotApplied(%v) = true, want false: the INCRBY may have landed", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("timeout after the write is ambiguous", func(t *testing.T) {
|
||||||
|
ln := newFakeListener(t)
|
||||||
|
stop := make(chan struct{})
|
||||||
|
t.Cleanup(func() { close(stop) })
|
||||||
|
go func() {
|
||||||
|
conn, err := ln.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() { _ = conn.Close() }()
|
||||||
|
r := bufio.NewReader(conn)
|
||||||
|
_, _ = readRESPCommand(r) // SELECT
|
||||||
|
_, _ = conn.Write([]byte("+OK\r\n"))
|
||||||
|
_, _ = readRESPCommand(r)
|
||||||
|
<-stop
|
||||||
|
}()
|
||||||
|
c := newRESPClient(ln.Addr().String(), "", 0)
|
||||||
|
_, err := c.pipeline(incr)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("want a timeout")
|
||||||
|
}
|
||||||
|
if respNotApplied(err) {
|
||||||
|
t.Errorf("respNotApplied(%v) = true, want false", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nil and foreign errors are not marked", func(t *testing.T) {
|
||||||
|
if respNotApplied(nil) || respNotApplied(fmt.Errorf("boom")) {
|
||||||
|
t.Error("unmarked error reported as not applied")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user