Skip to content

Commit

Permalink
Fix AtomicPosition::reset storing ms in prev
Browse files Browse the repository at this point in the history
The code actually expects this value to be in ns. I reckon the bug got
introduced because the comments are worded a bit ambiguously, so I also
cleared those up.
  • Loading branch information
TheJokr committed Jul 11, 2024
1 parent b462c70 commit 6853be9
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl AtomicPosition {
}

let mut capacity = self.capacity.load(Ordering::Acquire);
// `prev` is the number of ms after `self.started` we last returned `true`, in ns
// `prev` is the number of ns after `self.started` we last returned `true`
let prev = self.prev.load(Ordering::Acquire);
// `elapsed` is the number of ns since `self.started`
let elapsed = (now - self.start).as_nanos() as u64;
Expand All @@ -551,8 +551,8 @@ impl AtomicPosition {
return false;
}

// We now calculate `new`, the number of ms, in ns, since we last returned `true`,
// and `remainder`, which represents a number of ns less than 1ms which we cannot
// We now calculate `new`, the number of INTERVALs since we last returned `true`,
// and `remainder`, which represents a number of ns less than INTERVAL which we cannot
// convert into capacity now, so we're saving it for later. We do this by
// subtracting this from `elapsed` before storing it into `self.prev`.
let (new, remainder) = ((diff / INTERVAL), (diff % INTERVAL));
Expand All @@ -568,7 +568,7 @@ impl AtomicPosition {

fn reset(&self, now: Instant) {
self.set(0);
let elapsed = (now.saturating_duration_since(self.start)).as_millis() as u64;
let elapsed = (now.saturating_duration_since(self.start)).as_nanos() as u64;
self.prev.store(elapsed, Ordering::Release);
}

Expand Down

0 comments on commit 6853be9

Please sign in to comment.