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

@@ -0,0 +1,133 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: event_consumer.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const insertConsumerDeadLetter = `-- name: InsertConsumerDeadLetter :exec
INSERT INTO consumer_dead_letters (
consumer_name,
topic,
kafka_partition,
kafka_offset,
event_id,
event_type,
error_class,
reason,
attempts,
value_size,
value_sha256
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11
)
ON CONFLICT (
consumer_name, topic, kafka_partition, kafka_offset, value_sha256
) DO UPDATE SET
event_id = excluded.event_id,
event_type = excluded.event_type,
error_class = excluded.error_class,
reason = excluded.reason,
attempts = excluded.attempts,
dead_lettered_at = clock_timestamp()
`
type InsertConsumerDeadLetterParams struct {
ConsumerName string
Topic string
KafkaPartition int32
KafkaOffset int64
EventID pgtype.UUID
EventType pgtype.Text
ErrorClass string
Reason string
Attempts int32
ValueSize int32
ValueSha256 []byte
}
// Stores a record that a consumer could not apply. A second delivery of the
// same record (same position and value) refreshes the outcome of its row:
// after a rewind the handler may fail differently. A different value at a
// reused position, after the topic was recreated, gets its own row.
func (q *Queries) InsertConsumerDeadLetter(ctx context.Context, arg InsertConsumerDeadLetterParams) error {
_, err := q.db.Exec(ctx, insertConsumerDeadLetter,
arg.ConsumerName,
arg.Topic,
arg.KafkaPartition,
arg.KafkaOffset,
arg.EventID,
arg.EventType,
arg.ErrorClass,
arg.Reason,
arg.Attempts,
arg.ValueSize,
arg.ValueSha256,
)
return err
}
const insertProcessedEventMarker = `-- name: InsertProcessedEventMarker :execrows
INSERT INTO consumer_processed_events (consumer_name, event_id)
VALUES ($1, $2)
ON CONFLICT (consumer_name, event_id) DO NOTHING
`
type InsertProcessedEventMarkerParams struct {
ConsumerName string
EventID pgtype.UUID
}
// Records that a consumer applies an event, in the transaction of the effect.
// It returns 0 rows when the marker exists. While another transaction holds
// an uncommitted marker for the same key, the insert waits for it: after a
// commit it returns 0, after a rollback it inserts. So a concurrent duplicate
// never runs the effect at the same time as the first delivery.
func (q *Queries) InsertProcessedEventMarker(ctx context.Context, arg InsertProcessedEventMarkerParams) (int64, error) {
result, err := q.db.Exec(ctx, insertProcessedEventMarker, arg.ConsumerName, arg.EventID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const setConsumerTransactionTimeouts = `-- name: SetConsumerTransactionTimeouts :exec
SELECT
set_config('statement_timeout', $1::text, true),
set_config(
'idle_in_transaction_session_timeout',
$2::text,
true
)
`
type SetConsumerTransactionTimeoutsParams struct {
StatementTimeoutMs string
IdleTimeoutMs string
}
// Bounds a consumer transaction on the server, local to the transaction.
// statement_timeout stops a slow statement; idle_in_transaction_session_timeout
// ends the session of a process that froze or lost the network inside the
// handler, which releases the processed-event marker that a concurrent
// duplicate waits for.
func (q *Queries) SetConsumerTransactionTimeouts(ctx context.Context, arg SetConsumerTransactionTimeoutsParams) error {
_, err := q.db.Exec(ctx, setConsumerTransactionTimeouts, arg.StatementTimeoutMs, arg.IdleTimeoutMs)
return err
}
@@ -8,6 +8,28 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
type ConsumerDeadLetter struct {
ID int64
ConsumerName string
Topic string
KafkaPartition int32
KafkaOffset int64
EventID pgtype.UUID
EventType pgtype.Text
ErrorClass string
Reason string
Attempts int32
ValueSize int32
ValueSha256 []byte
DeadLetteredAt pgtype.Timestamptz
}
type ConsumerProcessedEvent struct {
ConsumerName string
EventID pgtype.UUID
ProcessedAt pgtype.Timestamptz
}
type EmailToken struct {
ID pgtype.UUID
UserID pgtype.UUID
@@ -49,6 +49,11 @@ type Querier interface {
GetPolicyVersionByID(ctx context.Context, id pgtype.UUID) (PolicyVersion, error)
GetUserByEmailNormalized(ctx context.Context, emailNormalized string) (User, error)
GetUserByID(ctx context.Context, id pgtype.UUID) (User, error)
// Stores a record that a consumer could not apply. A second delivery of the
// same record (same position and value) refreshes the outcome of its row:
// after a rewind the handler may fail differently. A different value at a
// reused position, after the topic was recreated, gets its own row.
InsertConsumerDeadLetter(ctx context.Context, arg InsertConsumerDeadLetterParams) error
InsertEmailToken(ctx context.Context, arg InsertEmailTokenParams) error
InsertOutboxEvent(ctx context.Context, arg InsertOutboxEventParams) error
// Stores a version and returns the stored row with the publication time and
@@ -57,6 +62,12 @@ type Querier interface {
// surrounding transaction. created_at is never taken from the caller, and a
// NULL effective_at means "at publication".
InsertPolicyVersion(ctx context.Context, arg InsertPolicyVersionParams) (PolicyVersion, error)
// Records that a consumer applies an event, in the transaction of the effect.
// It returns 0 rows when the marker exists. While another transaction holds
// an uncommitted marker for the same key, the insert waits for it: after a
// commit it returns 0, after a rollback it inserts. So a concurrent duplicate
// never runs the effect at the same time as the first delivery.
InsertProcessedEventMarker(ctx context.Context, arg InsertProcessedEventMarkerParams) (int64, error)
InsertSession(ctx context.Context, arg InsertSessionParams) error
InsertUser(ctx context.Context, arg InsertUserParams) error
InvalidateActiveEmailTokens(ctx context.Context, arg InvalidateActiveEmailTokensParams) (int64, error)
@@ -82,6 +93,12 @@ type Querier interface {
PolicyVersionExists(ctx context.Context, policyKey string) (bool, error)
RevokeSession(ctx context.Context, arg RevokeSessionParams) (int64, error)
RevokeUserSessions(ctx context.Context, arg RevokeUserSessionsParams) (int64, error)
// Bounds a consumer transaction on the server, local to the transaction.
// statement_timeout stops a slow statement; idle_in_transaction_session_timeout
// ends the session of a process that froze or lost the network inside the
// handler, which releases the processed-event marker that a concurrent
// duplicate waits for.
SetConsumerTransactionTimeouts(ctx context.Context, arg SetConsumerTransactionTimeoutsParams) error
// Bounds the claim transaction on the server. statement_timeout stops a
// statement that runs too long; idle_in_transaction_session_timeout ends the
// session, and with it the transaction and its row locks, when the client