kconsumer

package
v2.8.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPollTimeout = 100 * time.Millisecond

DefaultPollTimeout is default time that the kafka consumer polls for a new message before it checks the context for cancellation.

Functions

func RunConsumer added in v2.7.10

func RunConsumer(ctx context.Context, partitionConsumer *PartitionConsumer, handler HandleMessage)

RunConsumer runs partitionConsumer in a supervision loop that keeps it alive across failures. If Consume returns an error or panics, the panic is recovered, its stack is printed, and the consumer is restarted after a short delay. The loop only stops when ctx is canceled or the underlying kafka consumer is closed.

Types

type HandleMessage

type HandleMessage func(ctx context.Context, msg *kafka.Message) error

HandleMessage is the user-provided function called for each consumed message. It receives the raw *kafka.Message so the caller has access to key, headers, and partition/offset metadata.

type PartitionConsumer

type PartitionConsumer struct {
	Topics   []string
	Consumer *kafka.Consumer
}

PartitionConsumer subscribes to a topic and runs a dedicated goroutine per assigned partition. Offset storage is explicit: only messages that were successfully handled are stored for auto-commit.

If your handler returns an error, it is retried twice (three attempts in total). If it still fails, or if it panics, the affected worker stops and Consume shuts the whole consumer down, returning that error. A panic is recovered and reported like any other handler failure, so it never crashes the process or deadlocks the remaining partition workers.

func (*PartitionConsumer) Consume

func (c *PartitionConsumer) Consume(ctx context.Context, handle HandleMessage) error

Consume subscribes to the configured topics and dispatches every consumed message to handle. Each assigned partition is processed by its own worker goroutine, so messages within a partition are handled in order while different partitions run concurrently.

Offsets are stored only after a message was handled successfully and are flushed to the broker roughly every five seconds (and once more on shutdown). Consume blocks until ctx is canceled or a worker fails (because its handler exhausted its retries or panicked), in which case it shuts the workers down and returns an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL