feat(events): add idempotent Kafka event consumer runtime
This commit is contained in:
1 parent
565ef8d4f5
commit
3adb0ef9e4
47 files changed
+5018
-105
No files matched your search
@@ -1,40 +0,0 @@
|
||||
package outboxrelay
|
||||
|
||||
import (
|
||||
"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 broker, and the random half
|
||||
// spreads several relays 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.
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package outboxrelay
|
||||
|
||||
import (
|
||||
"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/worker/retry"
|
||||
)
|
||||
|
||||
// Metrics records relay outcomes and the backlog without per-event labels.
|
||||
@@ -27,7 +28,7 @@ type Settings struct {
|
||||
// PollInterval is the wait after a pass that found no pending event.
|
||||
PollInterval time.Duration
|
||||
// Backoff is the wait after a failed pass or a failed Prepare.
|
||||
Backoff Backoff
|
||||
Backoff retry.Backoff
|
||||
// ShutdownTimeout bounds how long a pass in progress may continue after
|
||||
// Run's context ends. When it expires the pass is canceled and its
|
||||
// transaction rolls back, so nothing it published is marked.
|
||||
@@ -155,7 +156,7 @@ func (relay *Relay) Run(ctx context.Context) {
|
||||
needsPrepare = errors.Is(err, outbound.ErrEventDestinationMissing)
|
||||
var delay time.Duration
|
||||
delay, failures = relay.nextDelay(result, err, failures)
|
||||
if !wait(ctx, delay) {
|
||||
if !retry.Wait(ctx, delay) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -207,7 +208,7 @@ func (relay *Relay) prepare(ctx context.Context, backlog *backlogRefresher) bool
|
||||
slog.Duration("retry_in", delay),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
if !wait(ctx, delay) {
|
||||
if !retry.Wait(ctx, delay) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -258,19 +259,3 @@ func (relay *Relay) passContext(ctx context.Context) (context.Context, func()) {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/worker/retry"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -136,7 +137,7 @@ func newTestRelay(store outbound.OutboxRelayStore, publisher outbound.EventPubli
|
||||
return New(store, publisher, metrics, slog.New(slog.DiscardHandler), Settings{
|
||||
BatchSize: 2,
|
||||
PollInterval: testInterval,
|
||||
Backoff: Backoff{Initial: testInterval, Max: 4 * testInterval},
|
||||
Backoff: retry.Backoff{Initial: testInterval, Max: 4 * testInterval},
|
||||
ShutdownTimeout: shutdownTimeout,
|
||||
BacklogInterval: testInterval,
|
||||
})
|
||||
@@ -314,7 +315,7 @@ func TestRunStopsDuringBackoffWithoutAnotherPass(t *testing.T) {
|
||||
}}, &countingMetrics{}, slog.New(slog.DiscardHandler), Settings{
|
||||
BatchSize: 1,
|
||||
PollInterval: testInterval,
|
||||
Backoff: Backoff{Initial: time.Hour, Max: time.Hour},
|
||||
Backoff: retry.Backoff{Initial: time.Hour, Max: time.Hour},
|
||||
ShutdownTimeout: time.Second,
|
||||
BacklogInterval: time.Hour,
|
||||
})
|
||||
@@ -337,7 +338,7 @@ func hourBackoffRelay(store outbound.OutboxRelayStore, publisher outbound.EventP
|
||||
return New(store, publisher, &countingMetrics{}, slog.New(slog.DiscardHandler), Settings{
|
||||
BatchSize: 2,
|
||||
PollInterval: testInterval,
|
||||
Backoff: Backoff{Initial: time.Hour, Max: time.Hour},
|
||||
Backoff: retry.Backoff{Initial: time.Hour, Max: time.Hour},
|
||||
ShutdownTimeout: time.Second,
|
||||
BacklogInterval: backlogInterval,
|
||||
})
|
||||
@@ -409,7 +410,7 @@ func TestRunRefreshesTheBacklogAtMostOncePerInterval(t *testing.T) {
|
||||
relay := New(store, fakePublisher{publish: acknowledgeAll}, metrics, slog.New(slog.DiscardHandler), Settings{
|
||||
BatchSize: 1,
|
||||
PollInterval: testInterval,
|
||||
Backoff: Backoff{Initial: testInterval, Max: testInterval},
|
||||
Backoff: retry.Backoff{Initial: testInterval, Max: testInterval},
|
||||
ShutdownTimeout: time.Second,
|
||||
BacklogInterval: time.Hour,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user