mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(resp): keep the not-applied flag through redisStore under yaegi
Yaegi drops the flag when fmt.Errorf wraps the error, and it has no errors.As. respNotApplied now checks the top-level type only. The redisStore wrapper marks its own error when the pipeline error is marked.
This commit is contained in:
+12
-2
@@ -227,6 +227,16 @@ func (s *redisStore) repairRecreatedExpires(entries []counterIncr, recreated []b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wrapStoreErr wraps a pipeline error with its operation name. A failure that never reached the wire stays marked
|
||||||
|
// as not applied, for the shared-state flushers.
|
||||||
|
func wrapStoreErr(op string, err error) error {
|
||||||
|
wrapped := fmt.Errorf("redisStore: %s: %w", op, err)
|
||||||
|
if respNotApplied(err) {
|
||||||
|
return ¬AppliedError{err: wrapped}
|
||||||
|
}
|
||||||
|
return wrapped
|
||||||
|
}
|
||||||
|
|
||||||
// applyIncrReplies decodes the INCRBY replies for entries — each entry's
|
// applyIncrReplies decodes the INCRBY replies for entries — each entry's
|
||||||
// own reply located via incrReplyIdx[i], not a fixed stride (a skipped
|
// own reply located via incrReplyIdx[i], not a fixed stride (a skipped
|
||||||
// EXPIRE shifts every later entry's commands left by one; see incrMulti/
|
// EXPIRE shifts every later entry's commands left by one; see incrMulti/
|
||||||
@@ -384,7 +394,7 @@ func (s *redisStore) incrMulti(entries []counterIncr) ([]int64, error) {
|
|||||||
|
|
||||||
replies, err := s.client.pipeline(cmds)
|
replies, err := s.client.pipeline(cmds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("redisStore: incrMulti: %w", err)
|
return nil, wrapStoreErr("incrMulti", err)
|
||||||
}
|
}
|
||||||
if len(replies) != len(cmds) {
|
if len(replies) != len(cmds) {
|
||||||
return nil, fmt.Errorf("redisStore: incrMulti: expected %d replies, got %d", len(cmds), len(replies))
|
return nil, fmt.Errorf("redisStore: incrMulti: expected %d replies, got %d", len(cmds), len(replies))
|
||||||
@@ -451,7 +461,7 @@ func (s *redisStore) incrAndGetMulti(entries []counterIncr, reads []string) ([]i
|
|||||||
|
|
||||||
replies, err := s.client.pipeline(cmds)
|
replies, err := s.client.pipeline(cmds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("redisStore: incrAndGetMulti: %w", err)
|
return nil, nil, wrapStoreErr("incrAndGetMulti", err)
|
||||||
}
|
}
|
||||||
if len(replies) != len(cmds) {
|
if len(replies) != len(cmds) {
|
||||||
return nil, nil, fmt.Errorf("redisStore: incrAndGetMulti: expected %d replies, got %d", len(cmds), len(replies))
|
return nil, nil, fmt.Errorf("redisStore: incrAndGetMulti: expected %d replies, got %d", len(cmds), len(replies))
|
||||||
|
|||||||
@@ -357,19 +357,11 @@ func markNotApplied(err error, wrote bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// respNotApplied reports whether err says the failed call never wrote its request.
|
// respNotApplied reports whether err says the failed call never wrote its request.
|
||||||
// It walks Unwrap by hand because errors.As is unsupported under yaegi.
|
// It checks the top-level type only: under yaegi a fmt.Errorf %w wrapper loses the marker, and errors.As is
|
||||||
|
// unsupported, so each wrapper that must keep it (redisStore) re-marks its own error.
|
||||||
func respNotApplied(err error) bool {
|
func respNotApplied(err error) bool {
|
||||||
for err != nil {
|
_, ok := err.(*notAppliedError)
|
||||||
if _, ok := err.(*notAppliedError); ok {
|
return 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
|
||||||
|
|||||||
+15
-3
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRESPPipelineNotApplied(t *testing.T) {
|
func TestRESPPipelineNotApplied(t *testing.T) {
|
||||||
@@ -21,9 +22,6 @@ func TestRESPPipelineNotApplied(t *testing.T) {
|
|||||||
if !respNotApplied(err) {
|
if !respNotApplied(err) {
|
||||||
t.Errorf("respNotApplied(%v) = false, want true", 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) {
|
t.Run("reply lost after bytes arrived is ambiguous", func(t *testing.T) {
|
||||||
@@ -84,3 +82,17 @@ func TestRESPPipelineNotApplied(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRedisStoreKeepsNotAppliedMarker(t *testing.T) {
|
||||||
|
ln := newFakeListener(t)
|
||||||
|
addr := ln.Addr().String()
|
||||||
|
_ = ln.Close()
|
||||||
|
s := newRedisStore(newRESPClient(addr, "", 0))
|
||||||
|
entries := []counterIncr{{key: "k", delta: 1, ttl: time.Minute, enforceTTL: time.Minute}}
|
||||||
|
if _, err := s.incrMulti(entries); err == nil || !respNotApplied(err) {
|
||||||
|
t.Errorf("incrMulti error %v: respNotApplied = false, want true for a dial failure", err)
|
||||||
|
}
|
||||||
|
if _, _, err := s.incrAndGetMulti(entries, nil); err == nil || !respNotApplied(err) {
|
||||||
|
t.Errorf("incrAndGetMulti error %v: respNotApplied = false, want true for a dial failure", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user