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

+86
View File
@@ -3,7 +3,9 @@ package config
import (
"errors"
"fmt"
"math"
"net"
"regexp"
"strconv"
"strings"
)
@@ -11,8 +13,35 @@ import (
const (
defaultKafkaBrokers = "localhost:9092"
kafkaBrokersSeparator = ","
// defaultKafkaEventsTopic receives every domain event. One topic keeps
// the setup small; the record key keeps each aggregate's events ordered
// within one partition.
defaultKafkaEventsTopic = "lincodex.domain.events"
// defaultKafkaEventsTopicPartitions lets up to three consumers of one
// group share the topic. Partitions can be added later but not removed,
// and adding them moves keys to other partitions.
defaultKafkaEventsTopicPartitions = 3
// maxKafkaEventsTopicPartitions bounds a mistyped value; one node does
// not benefit from more partitions.
maxKafkaEventsTopicPartitions = 1000
// KafkaBrokerDefaultReplicationFactor asks the broker to apply its own
// default.replication.factor when it creates the topic, so one setting on
// the broker covers both the single-node stand and a replicated cluster.
KafkaBrokerDefaultReplicationFactor = -1
// KafkaBrokerDefaultMinInsyncReplicas leaves min.insync.replicas of a
// created topic to the broker default.
KafkaBrokerDefaultMinInsyncReplicas = 0
// Topic provisioning is on by default because the broker never creates
// topics on demand.
defaultKafkaProvisionTopics = true
// maxKafkaTopicNameLength is the Kafka limit for topic names.
maxKafkaTopicNameLength = 249
)
// kafkaTopicNamePattern holds the characters Kafka accepts in a topic name.
var kafkaTopicNamePattern = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
// parseKafkaBrokers splits a comma-separated broker list. Empty entries are
// kept so that validation can reject values such as "a:9092,,b:9092".
func parseKafkaBrokers(raw string) []string {
@@ -27,6 +56,41 @@ func parseKafkaBrokers(raw string) []string {
return brokers
}
// loadKafkaTopics reads the events topic settings. An unset value takes its
// default; an empty or malformed value is an error.
func (config *Config) loadKafkaTopics(lookup LookupEnv) error {
config.KafkaEventsTopic = strings.TrimSpace(valueOrDefault(lookup, "KAFKA_EVENTS_TOPIC", defaultKafkaEventsTopic))
partitions, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_PARTITIONS", defaultKafkaEventsTopicPartitions)
if err != nil {
return err
}
if partitions < 1 || partitions > maxKafkaEventsTopicPartitions {
return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_PARTITIONS must be between 1 and %d", maxKafkaEventsTopicPartitions)
}
config.KafkaEventsTopicPartitions = int32(partitions)
replicationFactor, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR", KafkaBrokerDefaultReplicationFactor)
if err != nil {
return err
}
if replicationFactor < KafkaBrokerDefaultReplicationFactor || replicationFactor == 0 || replicationFactor > math.MaxInt16 {
return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR must be -1 for the broker default or between 1 and %d", math.MaxInt16)
}
config.KafkaEventsTopicReplicationFactor = int16(replicationFactor)
minInsyncReplicas, err := intOrDefault(lookup, "KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS", KafkaBrokerDefaultMinInsyncReplicas)
if err != nil {
return err
}
// An explicit 0 would read as the broker default, so only an unset
// variable selects it.
_, set := lookup("KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS")
if minInsyncReplicas < KafkaBrokerDefaultMinInsyncReplicas || minInsyncReplicas > math.MaxInt16 || (set && minInsyncReplicas == KafkaBrokerDefaultMinInsyncReplicas) {
return fmt.Errorf("Config.loadKafkaTopics - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must be between 1 and %d, or unset for the broker default", math.MaxInt16)
}
config.KafkaEventsTopicMinInsyncReplicas = int16(minInsyncReplicas)
config.KafkaProvisionTopics, err = boolOrDefault(lookup, "KAFKA_PROVISION_TOPICS", defaultKafkaProvisionTopics)
return err
}
func (config Config) ValidateKafka() error {
if len(config.KafkaBrokers) == 0 {
return errors.New("Config.ValidateKafka - KAFKA_BROKERS must not be empty")
@@ -38,9 +102,31 @@ func (config Config) ValidateKafka() error {
return fmt.Errorf("Config.ValidateKafka - KAFKA_BROKERS entry %d must be a host:port address with a nonzero port", index+1)
}
}
if !validKafkaTopicName(config.KafkaEventsTopic) {
return fmt.Errorf("Config.ValidateKafka - KAFKA_EVENTS_TOPIC must be 1 to %d letters, digits, dots, underscores, or hyphens other than a single or double dot", maxKafkaTopicNameLength)
}
if config.KafkaEventsTopicPartitions < 1 || config.KafkaEventsTopicPartitions > maxKafkaEventsTopicPartitions {
return fmt.Errorf("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_PARTITIONS must be between 1 and %d", maxKafkaEventsTopicPartitions)
}
if config.KafkaEventsTopicReplicationFactor != KafkaBrokerDefaultReplicationFactor && config.KafkaEventsTopicReplicationFactor < 1 {
return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR must be -1 for the broker default or positive")
}
if config.KafkaEventsTopicMinInsyncReplicas < KafkaBrokerDefaultMinInsyncReplicas {
return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must be positive or unset for the broker default")
}
// With an explicit replication factor, a larger min.insync.replicas would
// make every acks=all write fail.
if config.KafkaEventsTopicReplicationFactor > 0 && config.KafkaEventsTopicMinInsyncReplicas > config.KafkaEventsTopicReplicationFactor {
return errors.New("Config.ValidateKafka - KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS must not exceed KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR")
}
return nil
}
// validKafkaTopicName applies the broker's topic name rules.
func validKafkaTopicName(topic string) bool {
return len(topic) <= maxKafkaTopicNameLength && topic != "." && topic != ".." && kafkaTopicNamePattern.MatchString(topic)
}
// validKafkaBroker accepts a hostname or IP address with a port in 1-65535.
// URL schemes, user info, and paths are rejected because Kafka clients expect
// plain bootstrap addresses.