Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Fede1024 Rust rdkafka Custom Client Context

From Leeroopedia


Knowledge Sources
Domains Messaging, Design_Patterns
Last Updated 2026-02-07 19:00 GMT

Overview

A trait-based extension mechanism that allows users to inject custom callback logic into Kafka clients for logging, statistics, error handling, and consumer rebalance events.

Description

Custom Client Context implements the Strategy Pattern for Kafka client event handling. Rather than hardcoding behavior for log messages, statistics, errors, OAuth token generation, and consumer group rebalance events, the library defines traits (ClientContext and ConsumerContext) with default implementations. Users implement these traits on their own structs to customize behavior. The context is passed to create_with_context() at client construction time.

This pattern solves the problem of needing application-specific handling for Kafka events without modifying the library. Examples include custom logging to application-specific systems, tracking consumer group rebalance events for monitoring, and implementing OAuth token refresh for secured clusters.

Usage

Use custom client contexts when you need to:

  • Handle consumer group rebalance events (e.g., logging partition assignments)
  • Customize logging output from librdkafka
  • Process statistics from the Kafka client
  • Handle errors with application-specific logic
  • Implement OAuth token generation for SASL/OAUTHBEARER authentication

If no customization is needed, use the default context via create() instead of create_with_context().

Theoretical Basis

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable:

Pseudo-code logic:

// Abstract algorithm
trait ClientContext {
    fn log(level, facility, message)     // override for custom logging
    fn stats(statistics)                  // override for metrics collection
    fn error(error, reason)              // override for error handling
}

trait ConsumerContext: ClientContext {
    fn rebalance(consumer, event, partitions)  // override for rebalance logic
    fn commit_callback(result, offsets)         // override for commit tracking
}

context = MyContext::new()
client = config.create_with_context(context)

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment