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

@@ -1,6 +1,7 @@
package kafka
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
@@ -9,6 +10,7 @@ import (
"github.com/google/uuid"
"github.com/twmb/franz-go/pkg/kgo"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
@@ -74,3 +76,96 @@ func newRecord(topic string, event outbound.OutboxEvent) (*kgo.Record, error) {
},
}, nil
}
// decodeRecord turns a consumed record into an event. A value that is not a
// valid envelope yields a record whose Invalid error matches
// outbound.ErrInvalidEventRecord; the error names the broken rule and never
// quotes the value. EventID then comes from the event_id header when it holds
// a UUID, so an operator can still find the event.
func decodeRecord(record *kgo.Record) outbound.ConsumedRecord {
consumed := outbound.ConsumedRecord{
Topic: record.Topic,
Partition: record.Partition,
Offset: record.Offset,
Value: record.Value,
}
headerID, headerErr := uuid.Parse(recordHeader(record, headerEventID))
if headerErr == nil {
consumed.EventID = headerID
}
event, err := decodeEnvelope(record.Value)
if err == nil && headerErr == nil && headerID != event.EventID {
// A consumer that deduplicated by the header would disagree with one
// that reads the value, so the record is not trusted.
err = invalidRecord("event_id header differs from the envelope")
}
if err != nil {
consumed.Invalid = err
return consumed
}
consumed.Event = event
consumed.EventID = event.EventID
return consumed
}
func decodeEnvelope(value []byte) (outbound.ConsumedEvent, error) {
var decoded envelope
// The json error is dropped: its text can quote part of the value.
if err := json.Unmarshal(value, &decoded); err != nil {
return outbound.ConsumedEvent{}, invalidRecord("value is not a JSON event envelope")
}
if decoded.EnvelopeVersion != EnvelopeVersion {
return outbound.ConsumedEvent{}, invalidRecord("unsupported envelope_version")
}
if decoded.EventID == uuid.Nil {
return outbound.ConsumedEvent{}, invalidRecord("event_id is missing")
}
identity := domain.DomainEventInput{
EventType: decoded.EventType,
SchemaVersion: decoded.SchemaVersion,
AggregateType: decoded.AggregateType,
AggregateID: decoded.AggregateID,
AggregateVersion: decoded.AggregateVersion,
}
if err := identity.ValidateIdentity(); err != nil {
return outbound.ConsumedEvent{}, fmt.Errorf("kafka.decodeEnvelope - %w: %w", outbound.ErrInvalidEventRecord, err)
}
if decoded.OccurredAt.IsZero() {
return outbound.ConsumedEvent{}, invalidRecord("occurred_at is missing")
}
if !isJSONObject(decoded.Payload) {
return outbound.ConsumedEvent{}, invalidRecord("payload is not a JSON object")
}
return outbound.ConsumedEvent{
EventID: decoded.EventID,
EventType: decoded.EventType,
SchemaVersion: decoded.SchemaVersion,
AggregateType: decoded.AggregateType,
AggregateID: decoded.AggregateID,
AggregateVersion: decoded.AggregateVersion,
OccurredAt: decoded.OccurredAt,
Payload: decoded.Payload,
}, nil
}
// isJSONObject reports whether raw, which json.Unmarshal already checked, is
// an object rather than another value or null.
func isJSONObject(raw json.RawMessage) bool {
trimmed := bytes.TrimLeft(raw, " \t\r\n")
return len(trimmed) > 0 && trimmed[0] == '{'
}
func invalidRecord(reason string) error {
return fmt.Errorf("kafka.decodeEnvelope - %s: %w", reason, outbound.ErrInvalidEventRecord)
}
// recordHeader returns the last value of the header key, or "" without one.
func recordHeader(record *kgo.Record, key string) string {
value := ""
for _, header := range record.Headers {
if header.Key == key {
value = string(header.Value)
}
}
return value
}