use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ConsumerConfig {
pub group_id: Option<String>,
pub auto_commit: bool,
pub auto_commit_interval: Duration,
pub auto_offset_reset: AutoOffsetReset,
pub min_bytes: i32,
pub max_bytes: i32,
pub partition_max_bytes: i32,
pub max_wait: Duration,
pub max_poll_records: usize,
pub session_timeout: Duration,
pub rebalance_timeout: Duration,
pub heartbeat_interval: Duration,
pub partition_assignment_strategy: PartitionAssignmentStrategy,
pub enable_at_least_once: bool,
pub retries: i32,
pub retry_backoff: Duration,
}
impl ConsumerConfig {
pub fn new() -> Self {
Self {
group_id: None,
auto_commit: true,
auto_commit_interval: Duration::from_secs(5),
auto_offset_reset: AutoOffsetReset::Latest,
min_bytes: 1,
max_bytes: 50 * 1024 * 1024,
partition_max_bytes: 1024 * 1024,
max_wait: Duration::from_millis(500),
max_poll_records: 500,
session_timeout: Duration::from_secs(10),
rebalance_timeout: Duration::from_secs(30),
heartbeat_interval: Duration::from_secs(3),
partition_assignment_strategy: PartitionAssignmentStrategy::Range,
enable_at_least_once: false,
retries: 3,
retry_backoff: Duration::from_millis(200),
}
}
pub fn with_group_id(mut self, group_id: impl Into<String>) -> Self {
self.group_id = Some(group_id.into());
self
}
pub fn with_auto_commit(mut self, auto_commit: bool) -> Self {
self.auto_commit = auto_commit;
self
}
pub fn with_auto_commit_interval(mut self, interval: Duration) -> Self {
self.auto_commit_interval = interval;
self
}
pub fn with_auto_offset_reset(mut self, reset: AutoOffsetReset) -> Self {
self.auto_offset_reset = reset;
self
}
pub fn with_earliest(mut self) -> Self {
self.auto_offset_reset = AutoOffsetReset::Earliest;
self
}
pub fn with_latest(mut self) -> Self {
self.auto_offset_reset = AutoOffsetReset::Latest;
self
}
pub fn with_min_bytes(mut self, min_bytes: i32) -> Self {
self.min_bytes = min_bytes;
self
}
pub fn with_max_bytes(mut self, max_bytes: i32) -> Self {
self.max_bytes = max_bytes;
self
}
pub fn with_partition_max_bytes(mut self, partition_max_bytes: i32) -> Self {
self.partition_max_bytes = partition_max_bytes;
self
}
pub fn with_max_wait(mut self, max_wait: Duration) -> Self {
self.max_wait = max_wait;
self
}
pub fn with_max_poll_records(mut self, max_poll_records: usize) -> Self {
self.max_poll_records = max_poll_records;
self
}
pub fn with_session_timeout(mut self, timeout: Duration) -> Self {
self.session_timeout = timeout;
self
}
pub fn with_rebalance_timeout(mut self, timeout: Duration) -> Self {
self.rebalance_timeout = timeout;
self
}
pub fn with_heartbeat_interval(mut self, interval: Duration) -> Self {
self.heartbeat_interval = interval;
self
}
pub fn with_assignment_strategy(mut self, strategy: PartitionAssignmentStrategy) -> Self {
self.partition_assignment_strategy = strategy;
self
}
pub fn with_at_least_once(mut self) -> Self {
self.enable_at_least_once = true;
self
}
pub fn with_retries(mut self, retries: i32) -> Self {
self.retries = retries;
self
}
pub fn with_retry_backoff(mut self, backoff: Duration) -> Self {
self.retry_backoff = backoff;
self
}
}
impl Default for ConsumerConfig {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AutoOffsetReset {
Earliest,
Latest,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PartitionAssignmentStrategy {
Range,
RoundRobin,
CooperativeSticky,
}