feat(events): add idempotent Kafka event consumer runtime

This commit is contained in:
shira committed 2026-10-06 03:59:57 +03:00
1 parent 565ef8d4f5
commit 3adb0ef9e4
47 files changed
+5018 -105

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
// Package retry holds the waiting rules that the background workers share:
// a capped exponential backoff with jitter and a context-aware sleep.
package retry
import (
"context"
"math/rand/v2"
"time"
)
// Backoff is a capped exponential delay with equal jitter: the ceiling starts
// at Initial, doubles with every consecutive failure, and stops at Max; the
// delay is half the ceiling plus a random share of the other half. The fixed
// half keeps retries from hammering a recovering dependency, and the random
// half spreads several workers apart.
type Backoff struct {
Initial time.Duration
Max time.Duration
// jitter returns a value in [0, limit]. Tests replace it.
jitter func(limit time.Duration) time.Duration
}
// Delay returns the wait before the next attempt after failures consecutive
// failures. failures below 1 count as 1.
func (backoff Backoff) Delay(failures int) time.Duration {
ceiling := min(backoff.Initial, backoff.Max)
for attempt := 1; attempt < failures && ceiling < backoff.Max; attempt++ {
ceiling = min(ceiling*2, backoff.Max)
}
half := ceiling / 2
jitter := backoff.jitter
if jitter == nil {
jitter = randomJitter
}
return half + jitter(ceiling-half)
}
func randomJitter(limit time.Duration) time.Duration {
if limit <= 0 {
return 0
}
return rand.N(limit + 1) //nolint:gosec // Retry jitter needs no cryptographic randomness.
}
// Wait sleeps for delay and reports false when ctx ends first. A zero delay
// still reports a canceled context.
func Wait(ctx context.Context, delay time.Duration) bool {
if delay <= 0 {
return ctx.Err() == nil
}
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-ctx.Done():
return false
case <-timer.C:
return true
}
}
@@ -0,0 +1,71 @@
package retry
import (
"context"
"testing"
"time"
)
func TestBackoffDoublesFromInitialToTheCap(t *testing.T) {
noJitter := func(time.Duration) time.Duration { return 0 }
fullJitter := func(limit time.Duration) time.Duration { return limit }
backoff := Backoff{Initial: 500 * time.Millisecond, Max: 5 * time.Second}
ceilings := []time.Duration{
500 * time.Millisecond, // failure 1
time.Second,
2 * time.Second,
4 * time.Second,
5 * time.Second, // capped
5 * time.Second,
}
for index, ceiling := range ceilings {
failures := index + 1
backoff.jitter = noJitter
if got := backoff.Delay(failures); got != ceiling/2 {
t.Fatalf("Delay(%d) without jitter = %s, want %s", failures, got, ceiling/2)
}
backoff.jitter = fullJitter
if got := backoff.Delay(failures); got != ceiling {
t.Fatalf("Delay(%d) with full jitter = %s, want %s", failures, got, ceiling)
}
}
backoff.jitter = fullJitter
if got := backoff.Delay(0); got != 500*time.Millisecond {
t.Fatalf("Delay(0) = %s, want the first delay", got)
}
if got := backoff.Delay(1 << 20); got != 5*time.Second {
t.Fatalf("Delay(many) = %s, want the cap", got)
}
}
func TestBackoffRandomJitterStaysWithinTheCeiling(t *testing.T) {
backoff := Backoff{Initial: 100 * time.Millisecond, Max: time.Second}
for failures := 1; failures <= 8; failures++ {
ceiling := min(100*time.Millisecond<<(failures-1), time.Second)
for range 50 {
delay := backoff.Delay(failures)
if delay < ceiling/2 || delay > ceiling {
t.Fatalf("Delay(%d) = %s, want between %s and %s", failures, delay, ceiling/2, ceiling)
}
}
}
}
func TestWaitStopsWhenTheContextEnds(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
cancel()
if Wait(ctx, 0) {
t.Fatal("Wait(canceled, 0) = true, want false")
}
started := time.Now()
if Wait(ctx, time.Hour) {
t.Fatal("Wait(canceled, 1h) = true, want false")
}
if elapsed := time.Since(started); elapsed > time.Second {
t.Fatalf("Wait(canceled, 1h) took %s", elapsed)
}
if !Wait(t.Context(), time.Millisecond) {
t.Fatal("Wait(live, 1ms) = false, want true")
}
}