kafka_client_engine/config/
consumer_fetch.rs1use std::time::Duration;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct EngineConsumerFetchConfig {
10 max_wait: Duration,
11 min_bytes: usize,
12 max_bytes: usize,
13 partition_max_bytes: usize,
14 attempt_timeout: Duration,
15}
16
17impl EngineConsumerFetchConfig {
18 pub const fn new(
20 max_wait: Duration,
21 min_bytes: usize,
22 max_bytes: usize,
23 partition_max_bytes: usize,
24 attempt_timeout: Duration,
25 ) -> Self {
26 Self {
27 max_wait,
28 min_bytes,
29 max_bytes,
30 partition_max_bytes,
31 attempt_timeout,
32 }
33 }
34
35 pub const fn max_wait(self) -> Duration {
37 self.max_wait
38 }
39
40 pub const fn min_bytes(self) -> usize {
42 self.min_bytes
43 }
44
45 pub const fn max_bytes(self) -> usize {
47 self.max_bytes
48 }
49
50 pub const fn partition_max_bytes(self) -> usize {
52 self.partition_max_bytes
53 }
54
55 pub const fn attempt_timeout(self) -> Duration {
57 self.attempt_timeout
58 }
59
60 pub(crate) fn validate(self) -> Result<ValidatedConsumerFetchConfig, ConsumerFetchConfigError> {
61 let max_wait_ms =
62 whole_positive_milliseconds(self.max_wait).ok_or(ConsumerFetchConfigError::MaxWait)?;
63 let min_bytes =
64 i32::try_from(self.min_bytes).map_err(|_error| ConsumerFetchConfigError::MinBytes)?;
65 let max_bytes = i32::try_from(self.max_bytes)
66 .ok()
67 .filter(|value| *value > 0)
68 .ok_or(ConsumerFetchConfigError::MaxBytes)?;
69 let partition_max_bytes = i32::try_from(self.partition_max_bytes)
70 .ok()
71 .filter(|value| *value > 0)
72 .ok_or(ConsumerFetchConfigError::PartitionMaxBytes)?;
73 if min_bytes > max_bytes {
74 return Err(ConsumerFetchConfigError::MinBytesExceedMaxBytes);
75 }
76 if self.attempt_timeout.is_zero() || u64::try_from(self.attempt_timeout.as_nanos()).is_err()
77 {
78 return Err(ConsumerFetchConfigError::AttemptTimeout);
79 }
80 Ok(ValidatedConsumerFetchConfig {
81 max_wait_ms,
82 min_bytes: u32::try_from(min_bytes)
83 .unwrap_or_else(|_error| unreachable!("validated Fetch min bytes are nonnegative")),
84 max_bytes: u32::try_from(max_bytes)
85 .unwrap_or_else(|_error| unreachable!("validated Fetch max bytes are positive")),
86 partition_max_bytes: u32::try_from(partition_max_bytes).unwrap_or_else(|_error| {
87 unreachable!("validated partition Fetch max bytes are positive")
88 }),
89 attempt_timeout: self.attempt_timeout,
90 })
91 }
92}
93
94impl Default for EngineConsumerFetchConfig {
95 fn default() -> Self {
96 Self::new(
97 Duration::from_millis(500),
98 1,
99 1024 * 1024,
100 1024 * 1024,
101 Duration::from_secs(30),
102 )
103 }
104}
105
106#[derive(Clone, Copy, Debug, Eq, PartialEq)]
107pub(crate) struct ValidatedConsumerFetchConfig {
108 max_wait_ms: u32,
109 min_bytes: u32,
110 max_bytes: u32,
111 partition_max_bytes: u32,
112 attempt_timeout: Duration,
113}
114
115impl ValidatedConsumerFetchConfig {
116 pub(crate) const fn max_wait_ms(self) -> u32 {
117 self.max_wait_ms
118 }
119
120 pub(crate) const fn min_bytes(self) -> u32 {
121 self.min_bytes
122 }
123
124 pub(crate) const fn max_bytes(self) -> u32 {
125 self.max_bytes
126 }
127
128 pub(crate) const fn partition_max_bytes(self) -> u32 {
129 self.partition_max_bytes
130 }
131
132 pub(crate) const fn attempt_timeout(self) -> Duration {
133 self.attempt_timeout
134 }
135}
136
137impl Default for ValidatedConsumerFetchConfig {
138 fn default() -> Self {
139 Self {
140 max_wait_ms: 500,
141 min_bytes: 1,
142 max_bytes: 1024 * 1024,
143 partition_max_bytes: 1024 * 1024,
144 attempt_timeout: Duration::from_secs(30),
145 }
146 }
147}
148
149#[derive(Clone, Copy, Debug, Eq, PartialEq)]
150pub(crate) enum ConsumerFetchConfigError {
151 MaxWait,
152 MinBytes,
153 MaxBytes,
154 PartitionMaxBytes,
155 MinBytesExceedMaxBytes,
156 AttemptTimeout,
157}
158
159fn whole_positive_milliseconds(duration: Duration) -> Option<u32> {
160 if duration.is_zero() || duration.subsec_nanos() % 1_000_000 != 0 {
161 return None;
162 }
163 u32::try_from(duration.as_millis())
164 .ok()
165 .filter(|millis| i32::try_from(*millis).is_ok())
166}