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

+4 -19
View File
@@ -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
}
}