Kafka
Apache Kafka event streaming implementation with consumer groups, partitions, and TLS (SASL is not supported)
kafka
v2 note: Import
go.digitalxero.dev/mq-kafka/v2. Handlers returnerror(nil acks).AcknowledgeMessage/NacknowledgeMessagewere removed. SASL is not supported; usekafkas://ortls_enabledfor TLS. Consume key istopic(notqueue).initial_offsetisearliest/latest(oldest/newestaccepted as aliases).batch_timeoutis a duration string ("100ms").
| |
Index
- func AcknowledgeMessage(ctx context.Context, message types.Message) error
- func NacknowledgeMessage(ctx context.Context, message types.Message) error
- func NewKafkaConsumer(config *viper.Viper) (types.Consumer, error)
- func NewKafkaProducer(config *viper.Viper) (types.Producer, error)
- func NewKafkaTransport() types.Transport
- func NewMessageBuilder() types.MessageBuilder
- type ConfigurationError
- type ConfigurationErrors
func AcknowledgeMessage
| |
AcknowledgeMessage provides a robust way for handlers to acknowledge messages with retry logic This function is exported to allow handlers to use the same acknowledgment retry mechanism
func NacknowledgeMessage
| |
NacknowledgeMessage provides a robust way for handlers to nack messages with retry logic This function is exported to allow handlers to use the same nack retry mechanism
func NewKafkaConsumer
| |
NewKafkaConsumer creates a new Apache Kafka consumer with the provided configuration. This function is typically called by mqutils.NewConsumer when it detects a Kafka URL.
Configuration options:
- url: Kafka broker URLs (required, e.g., “kafka://broker1:9092,broker2:9092”)
- topic: Topic name to consume from (required; alias: destination)
- consumer_group: Consumer group ID (required; default: “mqutils-default-group”). This value is now applied to the Sarama group session.
- handler: Name of registered handler function (default: “kafkaLogger”)
- tls_enabled: Enable TLS encryption (default: false; also enabled by kafkas://). SASL is not supported.
- skip_verify: Skip TLS certificate verification (default: false)
- retry_topic: Topic for message retries (optional; required for nack without losing offsets)
- retry_max_retries: Max retry attempts (default: 50; alias: max_retries)
- session_timeout: Session timeout in ms (default: 30000)
- heartbeat_interval: Heartbeat interval in ms (default: 3000)
- initial_offset: Where to start reading - earliest/oldest or latest/newest (default: latest; alias: auto_offset_reset)
- auto_commit: Auto-commit offsets (default: true; alias: enable_auto_commit)
- batch_size: Number of messages per batch (default: 5)
- batch_timeout: Batch collection timeout as a duration string (default: “100ms”)
- enable_batch_processing: Enable batch message processing (default: false)
Returns an error if configuration validation fails or transport creation fails.
func NewKafkaProducer
| |
NewKafkaProducer creates a new Apache Kafka producer with the provided configuration. This function is typically called by mqutils.NewProducer when it detects a Kafka URL.
Configuration options:
- url: Kafka broker URLs (required, e.g., “kafka://broker1:9092,broker2:9092”)
- topic: Default publish topic (optional; alias: destination). Used when Publish is called with an empty topic.
- tls_enabled: Enable TLS encryption (default: false; also enabled by kafkas://). SASL is not supported.
- skip_verify: Skip TLS certificate verification (default: false)
- compression_type: Compression - none, gzip, snappy, lz4 (default: snappy)
- required_acks: Ack mode - all, 1, 0 (default: all)
- max_retries: Max retry attempts for failed sends (default: 3)
- retry_backoff: Retry backoff in ms (default: 100)
- flush_frequency: Flush interval in ms (default: 10)
- flush_messages: Messages before flush (default: 100)
- flush_bytes: Bytes before flush (default: 1048576)
- partitioner: Partitioning strategy - hash, random, manual (default: hash)
Returns an error if configuration validation fails or transport creation fails. The producer must be started with Start() before publishing messages.
func NewKafkaTransport
| |
NewKafkaTransport creates a new Kafka transport implementation. The transport provides low-level Kafka operations including connection management, producer/consumer creation, and message operations.
The transport manages:
- Kafka client connections with automatic broker discovery
- Producer instances for publishing
- Consumer group coordination
- TLS encryption (kafkas:// or tls_enabled). SASL is not supported.
- Connection health monitoring
Unlike AMQP, Kafka doesn’t use channels, so this transport manages Sarama client and producer instances directly.
func NewMessageBuilder
| |
NewMessageBuilder creates a new Kafka message builder. The builder provides a fluent interface for constructing Kafka messages with all supported properties and attributes.
Example:
msg := kafka.NewMessageBuilder().
WithBody([]byte(`{"event": "order.placed"}`)).
WithContentType("application/json").
WithRoutingKey("order-123"). // Used as partition key
WithHeaders(map[string]interface{}{
"source": "order-service",
"version": "2.0",
}).
Build()
type ConfigurationError
ConfigurationError represents a validation error with specific field and message
| |
func (*ConfigurationError) Error
| |
type ConfigurationErrors
ConfigurationErrors represents multiple validation errors
| |
func (*ConfigurationErrors) Add
| |
Add adds a new configuration error to the collection
func (*ConfigurationErrors) Error
| |
func (*ConfigurationErrors) HasErrors
| |
HasErrors returns true if there are any validation errors
Generated by gomarkdoc