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

@@ -81,7 +81,7 @@ func (store *OutboxRelayStore) ClaimPending(ctx context.Context, limit int, publ
return claimErr
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("OutboxRelayStore.ClaimPending - commit transaction: %w", relayDatabaseError(err))
return fmt.Errorf("OutboxRelayStore.ClaimPending - commit transaction: %w", sanitizedDatabaseError(err))
}
return nil
}
@@ -93,11 +93,11 @@ func claimPending(ctx context.Context, tx pgx.Tx, limit int32, timeouts OutboxRe
IdleTimeoutMs: milliseconds(timeouts.IdleInTransaction),
})
if err != nil {
return fmt.Errorf("postgres.claimPending - set transaction timeouts: %w", relayDatabaseError(err))
return fmt.Errorf("postgres.claimPending - set transaction timeouts: %w", sanitizedDatabaseError(err))
}
rows, err := queries.LockPendingOutboxEvents(ctx, limit)
if err != nil {
return fmt.Errorf("postgres.claimPending - lock pending events: %w", relayDatabaseError(err))
return fmt.Errorf("postgres.claimPending - lock pending events: %w", sanitizedDatabaseError(err))
}
if len(rows) == 0 {
return nil
@@ -130,7 +130,7 @@ func claimPending(ctx context.Context, tx pgx.Tx, limit int32, timeouts OutboxRe
}
count, err := queries.MarkOutboxEventsPublished(ctx, marked)
if err != nil {
return fmt.Errorf("postgres.claimPending - mark published events: %w", relayDatabaseError(err))
return fmt.Errorf("postgres.claimPending - mark published events: %w", sanitizedDatabaseError(err))
}
if count != int64(len(marked)) {
return fmt.Errorf("postgres.claimPending - marked %d events, want %d", count, len(marked))
@@ -142,7 +142,7 @@ func claimPending(ctx context.Context, tx pgx.Tx, limit int32, timeouts OutboxRe
func (store *OutboxRelayStore) Backlog(ctx context.Context) (outbound.OutboxBacklog, error) {
row, err := sqlc.New(store.pool).GetOutboxBacklog(ctx)
if err != nil {
return outbound.OutboxBacklog{}, fmt.Errorf("OutboxRelayStore.Backlog - read backlog: %w", relayDatabaseError(err))
return outbound.OutboxBacklog{}, fmt.Errorf("OutboxRelayStore.Backlog - read backlog: %w", sanitizedDatabaseError(err))
}
return outbound.OutboxBacklog{
Pending: row.PendingEvents,
@@ -169,10 +169,11 @@ func outboxEventFromRow(row sqlc.LockPendingOutboxEventsRow) outbound.OutboxEven
}
}
// relayDatabaseError keeps only the SQLSTATE of a PostgreSQL error, whose
// message or detail could quote an event payload, and drops the connection
// prefix of a connect error.
func relayDatabaseError(err error) error {
// sanitizedDatabaseError keeps only the SQLSTATE and constraint of a
// PostgreSQL error, whose message or detail could quote an event payload, and
// drops the connection prefix of a connect error. The relay and the event
// consumers use it.
func sanitizedDatabaseError(err error) error {
var pgError *pgconn.PgError
if errors.As(err, &pgError) {
return markTransactionConflict(&databaseError{code: pgError.Code, constraint: pgError.ConstraintName})