feat: repair matcher candidates through redis projection

This commit is contained in:
Josh Creek
2026-09-01 10:11:54 +01:00
parent 7a3d520608
commit d882469c79
5 changed files with 82 additions and 3 deletions
+25
View File
@@ -46,3 +46,28 @@ func TestCandidateProjectionDoesNotReturnCacheWhenRepairSourceFails(t *testing.T
t.Fatal("cache projection succeeded without a usable Redis/index source")
}
}
func TestCandidateProjectionRepairsEmptyIndexFromDurableSource(t *testing.T) {
mini, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mini.Close()
client := redis.NewClient(&redis.Options{Addr: mini.Addr()})
defer client.Close()
now := time.Unix(1000, 0).UTC()
candidate := domain.Candidate{TicketID: "miss-ticket", PlayerID: "miss-player", EnqueuedAt: now}
projection := CandidateProjection{
Index: RedisCandidateIndex{Client: client, Prefix: "miss", TTL: time.Minute},
Source: func(context.Context, time.Time) ([]domain.Candidate, error) {
return []domain.Candidate{candidate}, nil
},
}
got, err := projection.Snapshot(context.Background(), now)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].TicketID != candidate.TicketID {
t.Fatalf("empty-index repair = %+v", got)
}
}
+9
View File
@@ -49,6 +49,15 @@ func (p CandidateProjection) Snapshot(ctx context.Context, now time.Time) ([]dom
}
candidates, err := p.Index.Snapshot(ctx, now)
if err == nil {
// An empty index is indistinguishable from a Redis restart or a lost
// keyspace. Rebuild from PostgreSQL before returning so queued players
// are not hidden until the next enqueue mutation.
if len(candidates) == 0 {
if err := p.Repair(ctx, now); err != nil {
return nil, err
}
return p.Index.Snapshot(ctx, now)
}
return candidates, nil
}
if err := p.Repair(ctx, now); err != nil {