Implementation:Fede1024 Rust rdkafka ClientConfig Create With Context
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Messaging, Configuration |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete context-aware client creation method provided by rust-rdkafka's ClientConfig.
Description
ClientConfig::create_with_context delegates to the FromClientConfigAndContext trait implementation of the target type. For StreamConsumer, this is at src/consumer/stream_consumer.rs:L221-272. The construction process:
- Creates a native config via create_native_config()
- Extracts max.poll.interval.ms for the background wake loop
- Creates a BaseConsumer with the native config and context
- Sets up a WakerSlab for async waker management
- Enables the queue nonempty callback for efficient wakeups
- Spawns a background task that periodically wakes all streams
Usage
Call on a configured ClientConfig with the custom context instance and the desired consumer type as the type parameter.
Code Reference
Source Location
- Repository: rust-rdkafka
- File: src/config.rs (create_with_context method), src/consumer/stream_consumer.rs (FromClientConfigAndContext impl)
- Lines: config.rs:L310-316, stream_consumer.rs:L221-272
Signature
impl ClientConfig {
pub fn create_with_context<C, T>(&self, context: C) -> KafkaResult<T>
where
C: ClientContext,
T: FromClientConfigAndContext<C>;
}
// StreamConsumer's FromClientConfigAndContext impl
impl<C, R> FromClientConfigAndContext<C> for StreamConsumer<C, R>
where
C: ConsumerContext + 'static,
R: AsyncRuntime,
{
fn from_config_and_context(config: &ClientConfig, context: C) -> KafkaResult<Self>;
}
Import
use rdkafka::config::ClientConfig;
use rdkafka::consumer::StreamConsumer;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| &self | &ClientConfig | Yes | Configured ClientConfig |
| context | C: ClientContext | Yes | Custom context implementing ClientContext/ConsumerContext |
| T (type param) | FromClientConfigAndContext<C> | Yes | Target type (e.g., StreamConsumer<C>) |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | KafkaResult<T> | Configured client with custom context or error |
Usage Examples
Create StreamConsumer with Custom Context
use rdkafka::config::ClientConfig;
use rdkafka::consumer::StreamConsumer;
let consumer: StreamConsumer<MyContext> = ClientConfig::new()
.set("bootstrap.servers", "localhost:9092")
.set("group.id", "my-group")
.create_with_context(MyContext::new())
.expect("Consumer creation failed");
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment