Documentation
¶
Overview ¶
Package kafka provides a Kafka backend for the ling-base/mq broker abstraction. It uses github.com/segmentio/kafka-go for transport and maps the mq.Broker/Producer/Consumer interfaces onto Kafka topics and consumer groups.
Kafka has no exchanges, queues, or bindings. Topics play the role of exchanges, partitions play the role of routing keys, and consumer groups play the role of queues. The topology methods (DeclareQueue, Bind, Unbind, DeleteQueue, DeleteExchange) are therefore no-ops that return nil. DeclareExchange optionally creates a Kafka topic.
Index ¶
- type Broker
- func (b *Broker) Bind(queue, exchange, routingKey string) error
- func (b *Broker) Close() error
- func (b *Broker) Connect() error
- func (b *Broker) Consumer(topic string, opts mq.ConsumeOptions) (mq.Consumer, error)
- func (b *Broker) DeclareExchange(name string, opts mq.ExchangeOptions) error
- func (b *Broker) DeclareQueue(name string, opts mq.QueueOptions) error
- func (b *Broker) DeleteExchange(name string) error
- func (b *Broker) DeleteQueue(name string) error
- func (b *Broker) IsConnected() bool
- func (b *Broker) Metrics() mq.Metrics
- func (b *Broker) Producer(topic string, opts mq.PublishOptions) (mq.Producer, error)
- func (b *Broker) Unbind(queue, exchange, routingKey string) error
- type Config
- type Consumer
- type Producer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker implements mq.Broker for Kafka.
func New ¶
New creates a new Kafka broker. Call Connect() to verify broker reachability (optional — Kafka connections are per-operation).
func (*Broker) Bind ¶
Bind is a no-op for Kafka. Kafka routes by topic and partition, not by exchange/queue bindings.
func (*Broker) Connect ¶
Connect verifies that at least one broker is reachable. For Kafka, connections are established per-operation by the reader/writer, so this is optional. It performs a quick metadata request.
func (*Broker) Consumer ¶
Consumer creates a consumer for the given topic with the given options. The consumer is not started; call Start to begin.
If topic is empty, the broker's default Topic is used. The consumer group ID is taken from ConsumeOptions.ConsumerTag if non-empty, otherwise the broker's default GroupID.
func (*Broker) DeclareExchange ¶
func (b *Broker) DeclareExchange(name string, opts mq.ExchangeOptions) error
DeclareExchange creates a Kafka topic if it does not already exist. Kafka has no exchanges; topics serve as the publish target.
func (*Broker) DeclareQueue ¶
func (b *Broker) DeclareQueue(name string, opts mq.QueueOptions) error
DeclareQueue is a no-op for Kafka. Kafka has no queues; consumer groups serve as the subscription unit.
func (*Broker) DeleteExchange ¶
DeleteExchange deletes a Kafka topic.
func (*Broker) DeleteQueue ¶
DeleteQueue is a no-op for Kafka.
func (*Broker) IsConnected ¶
IsConnected reports whether the broker has brokers configured. For Kafka, connections are per-operation, so this is a lightweight check.
type Config ¶
type Config struct {
// Brokers is the list of Kafka bootstrap servers.
// Example: []string{"localhost:9092"}
Brokers []string
// Topic is the default topic used when a producer or consumer is
// created without an explicit topic. May be empty if topics are
// always passed explicitly to Producer/Consumer.
Topic string
// GroupID is the default consumer group ID. May be overridden per
// consumer via ConsumeOptions.ConsumerTag.
GroupID string
// DialerTimeout is the timeout for establishing connections.
// Default: 10s.
DialerTimeout time.Duration
// EnableTLS enables TLS for all connections.
EnableTLS bool
// TLSConfig is the TLS configuration. If EnableTLS is true and
// TLSConfig is nil, a default (InsecureSkipVerify=false) config is
// used.
TLSConfig *tls.Config
// CommitInterval is the auto-commit interval for consumers.
// Default: 1s. Set to 0 to disable auto-commit (manual commit only).
CommitInterval time.Duration
}
Config configures a Kafka broker.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults for localhost.
type Consumer ¶
type Consumer struct {
// contains filtered or unexported fields
}
Consumer implements mq.Consumer for Kafka using a kafka.Reader.
func (*Consumer) Start ¶
Start begins consuming from the Kafka topic. The consumer runs until ctx is cancelled or Stop is called.