Principle:Fede1024 Rust rdkafka Consumer Creation With Context
| Knowledge Sources | |
|---|---|
| Domains | Messaging, Configuration |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
A configuration pattern for creating Kafka consumers with custom context objects that provide application-specific callback behavior.
Description
Consumer Creation With Context extends the basic client configuration pattern by accepting a user-provided context object. The context implements ClientContext and/or ConsumerContext traits, enabling custom handling of logs, statistics, errors, and consumer group rebalance events.
This is distinct from the basic create() method which uses a DefaultConsumerContext with no customization. The create_with_context method uses Rust's trait system to ensure the context type matches the client type being created.
The StreamConsumer construction (via FromClientConfigAndContext) also sets up the async wake loop, extracts max.poll.interval.ms for background polling, and configures the internal WakerSlab for efficient async notification.
Usage
Use when you need custom rebalance handling, logging, statistics collection, or OAuth token generation in your consumer. Pass your context struct to create_with_context() after implementing the relevant traits.
Theoretical Basis
Pseudo-code logic:
// Abstract algorithm
struct MyContext;
impl ClientContext for MyContext { ... }
impl ConsumerContext for MyContext { ... }
consumer = ClientConfig::new()
.set("bootstrap.servers", brokers)
.set("group.id", group)
.create_with_context::<MyContext, StreamConsumer<MyContext>>(MyContext)