Skip to content

Commit

Permalink
Fix op timeout computation logic on poll_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
scanterog committed Nov 7, 2023
1 parent 3b13940 commit 978c964
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/consumer/base_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::mem::ManuallyDrop;
use std::os::raw::c_void;
use std::ptr;
use std::sync::Arc;
use std::time::Duration;
use std::time::{Duration, Instant};

use log::{error, warn};
use rdkafka_sys as rdsys;
Expand Down Expand Up @@ -123,6 +123,7 @@ where
queue: &NativeQueue,
timeout: T,
) -> Option<KafkaResult<BorrowedMessage<'_>>> {
let now = Instant::now();
let mut timeout = timeout.into();
let min_poll_interval = self.context().main_queue_min_poll_interval();
loop {
Expand Down Expand Up @@ -158,10 +159,10 @@ where
}
}

if op_timeout >= timeout {
timeout = timeout.saturating_sub(now.elapsed());
if timeout.is_zero() {
return None;
}
timeout -= op_timeout;
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ impl Timeout {
Timeout::Never => -1,
}
}

/// Saturating `Duration` subtraction to Timeout.
pub(crate) fn saturating_sub(&self, rhs: Duration) -> Timeout {
match (self, rhs) {
(Timeout::After(lhs), rhs) => Timeout::After(lhs.saturating_sub(rhs)),
(Timeout::Never, _) => Timeout::Never,
}
}

/// Returns `true` if the timeout is zero.
pub(crate) fn is_zero(&self) -> bool {
match self {
Timeout::After(d) => d.is_zero(),
Timeout::Never => false,
}
}
}

impl std::ops::SubAssign for Timeout {
Expand Down

0 comments on commit 978c964

Please sign in to comment.