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:
2026-11-26 04:06:34 +00:00
parent 648848e23a
commit b1fa1bee14
2 changed files with 120 additions and 4 deletions
+34 -4
View File
@@ -318,7 +318,7 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any
pc, err := c.acquire(deadline)
if err != nil {
return nil, err
return nil, &notAppliedError{err: err}
}
defer func() { c.free <- pc }()
@@ -328,20 +328,50 @@ func (c *respClient) pipelineEncoded(payload []byte, n int, mutates bool) ([]any
}
closeConn(pc)
if isTimeout(err) {
return nil, err
return nil, markNotApplied(err, wrote)
}
if wrote && readAny && mutates {
return nil, err
}
replies, _, _, err = c.attemptEncodedOn(pc, payload, n, deadline)
replies, wrote, _, err = c.attemptEncodedOn(pc, payload, n, deadline)
if err != nil {
closeConn(pc)
return nil, err
return nil, markNotApplied(err, wrote)
}
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 &notAppliedError{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
// that is NOT safe to blindly resend: each already changes server state on
// success, so replaying it after an ambiguous failure (the request bytes