Documentation
¶
Overview ¶
Package kafka is a pure-Go (CGO-free), MRI-faithful reimplementation of the Ruby ruby-kafka gem's client surface — the object model a Ruby program drives when it calls Kafka.new, kafka.producer, kafka.consumer and the topic admin helpers.
It mirrors the gem's method names and semantics:
- New returns a Kafka client, the analogue of Kafka.new(seed_brokers:, client_id:).
- Kafka.Producer returns a buffering Producer; #produce buffers a message and #deliver_messages flushes the buffer. Kafka.DeliverMessage is the one-shot synchronous send.
- Kafka.Consumer returns a consumer-group Consumer; #subscribe registers topics and #each_message / #each_batch drive the poll loop, with explicit #commit_offsets and #stop.
- Kafka.CreateTopic, Kafka.Topics, Kafka.PartitionsFor and Kafka.DeleteTopic are the admin surface.
The Kafka wire protocol is NOT reimplemented here. This package consumes the pure-Go franz-go client (kgo + kadm) for the protocol and, in its tests, the in-process kfake broker for deterministic round-trips with no external Kafka. The broker connection is a host seam: the constructors that dial a broker are package variables, so tests inject either the real franz-go client (pointed at kfake) or a deterministic mock.
Index ¶
- Variables
- type Batch
- type Consumer
- type Kafka
- func (k *Kafka) Close()
- func (k *Kafka) Consumer(groupID string) *Consumer
- func (k *Kafka) CreateTopic(name string, numPartitions int32, replicationFactor int16) error
- func (k *Kafka) DeleteTopic(name string) error
- func (k *Kafka) DeliverMessage(value []byte, topic string) error
- func (k *Kafka) PartitionsFor(name string) ([]int32, error)
- func (k *Kafka) Producer() *Producer
- func (k *Kafka) Topics() ([]string, error)
- type Message
- type Options
- type ProduceOptions
- type Producer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDeliveryFailed is Kafka::DeliveryFailed — a produce did not succeed. ErrDeliveryFailed = errors.New("kafka: delivery failed") // ErrConnection is Kafka::ConnectionError — the broker could not be reached // or a request failed at the transport level. ErrConnection = errors.New("kafka: connection error") // ErrUnknownTopicOrPartition is Kafka::UnknownTopicOrPartition. ErrUnknownTopicOrPartition = errors.New("kafka: unknown topic or partition") // ErrOffsetCommit is Kafka::OffsetCommitError — committing consumer-group // offsets failed. ErrOffsetCommit = errors.New("kafka: offset commit error") // ErrOffsetOutOfRange is Kafka::OffsetOutOfRange — a fetch requested an // offset the broker no longer holds. ErrOffsetOutOfRange = errors.New("kafka: offset out of range") )
The Kafka error tree, mirroring ruby-kafka's Kafka::*Error classes. Every error returned by this package wraps one of these sentinels, so callers can classify failures with errors.Is, e.g.:
if errors.Is(err, kafka.ErrUnknownTopicOrPartition) { ... }
Functions ¶
This section is empty.
Types ¶
type Batch ¶
Batch is a per-(topic, partition) run of messages yielded by Consumer.EachBatch, mirroring ruby-kafka's batch object.
type Consumer ¶
type Consumer struct {
// contains filtered or unexported fields
}
Consumer is a consumer-group member, the analogue of the object returned by kafka.consumer(group_id:). Topics are registered with Consumer.Subscribe; the poll loop is driven by Consumer.EachMessage or Consumer.EachBatch; offsets are persisted with Consumer.CommitOffsets; and Consumer.Stop ends the loop.
func (*Consumer) CommitOffsets ¶
CommitOffsets persists the offsets of the messages yielded so far, mirroring consumer.commit_offsets. It is a no-op if nothing has been consumed.
func (*Consumer) EachBatch ¶
EachBatch runs the poll loop, yielding a per-partition batch at a time, mirroring consumer.each_batch { |batch| ... }.
func (*Consumer) EachMessage ¶
EachMessage runs the poll loop, yielding one message at a time, mirroring consumer.each_message { |message| ... }. It returns nil after Consumer.Stop, or the error the block returns, or a connection/offset error.
func (*Consumer) Stop ¶
func (c *Consumer) Stop()
Stop ends the poll loop, mirroring consumer.stop. It is safe to call from any goroutine.
type Kafka ¶
type Kafka struct {
// contains filtered or unexported fields
}
Kafka is the top-level client, the analogue of the object returned by Kafka.new. It is the factory for producers and consumers and carries the topic-admin surface. A lazily-created admin connection backs the admin methods and is released by Kafka.Close.
func New ¶
New builds a Kafka client. It performs no I/O; connections are opened lazily by the producer, consumer and admin surfaces.
func (*Kafka) Close ¶
func (k *Kafka) Close()
Close releases the shared admin connection, if one was opened.
func (*Kafka) Consumer ¶
Consumer returns a new consumer-group consumer, mirroring kafka.consumer(group_id:).
func (*Kafka) CreateTopic ¶
CreateTopic creates a topic, mirroring kafka.create_topic(name, num_partitions:, replication_factor:).
func (*Kafka) DeleteTopic ¶
DeleteTopic deletes a topic, mirroring kafka.delete_topic(name).
func (*Kafka) DeliverMessage ¶
DeliverMessage sends a single message synchronously, mirroring kafka.deliver_message(value, topic:). It opens a short-lived producer, sends, and closes it.
func (*Kafka) PartitionsFor ¶
PartitionsFor returns the partition ids of a topic, mirroring kafka.partitions_for(name). An unknown topic yields ErrUnknownTopicOrPartition.
type Message ¶
type Message struct {
Topic string
Partition int32
Offset int64
Key []byte
Value []byte
Headers map[string][]byte
CreateTime time.Time
}
Message is a consumed record, the analogue of ruby-kafka's message object. Its Topic field plays the "subject" role: it names where the message came from, alongside the partition/offset coordinates and the payload.
type Options ¶
type Options struct {
// SeedBrokers is the list of "host:port" bootstrap brokers.
SeedBrokers []string
// ClientID is the client identifier sent to the broker. Optional.
ClientID string
}
Options configures a Kafka client, mirroring the keyword arguments of the gem's Kafka.new(seed_brokers:, client_id:).
type ProduceOptions ¶
type ProduceOptions struct {
// Topic is the destination topic. Required.
Topic string
// Key is the optional partitioning/message key.
Key []byte
// Partition, when non-nil, pins the message to an explicit partition;
// otherwise the partition is chosen by key hash (or balanced).
Partition *int32
// Headers is the optional set of record headers.
Headers map[string][]byte
}
ProduceOptions mirrors the keyword arguments of ruby-kafka's producer.produce(value, topic:, key:, partition:, headers:).
type Producer ¶
type Producer struct {
// contains filtered or unexported fields
}
Producer is a buffering producer, the analogue of the object returned by kafka.producer. Messages are buffered by Producer.Produce and flushed by Producer.DeliverMessages; Producer.Shutdown releases the connection.
func (*Producer) DeliverMessages ¶
DeliverMessages flushes the buffered messages to Kafka, mirroring producer.deliver_messages. An empty buffer is a no-op. On success the buffer is cleared; on failure it is retained for a later retry.
