feat(outbox): relay committed outbox events to Kafka

This commit is contained in:
shira committed 2026-10-06 03:01:53 +03:00
1 parent 78f1651f6b
commit 6c3b34f73e
42 files changed
+3731 -45

No files matched your search

@@ -0,0 +1,40 @@
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.
}