Skip to content

Commit

Permalink
feat(s2n-quic-core): add Cached clock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Feb 20, 2024
1 parent 14023eb commit cc8d04f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
36 changes: 36 additions & 0 deletions quic/s2n-quic-core/src/time/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,39 @@ impl Clock for NoopClock {
unsafe { Timestamp::from_duration(Duration::from_micros(1)) }
}
}

impl Clock for Timestamp {
#[inline]
fn get_time(&self) -> Timestamp {
*self
}
}

/// A clock that caches the time query for the inner clock
pub struct Cached<'a, C: Clock> {
clock: &'a C,
cached_value: core::cell::Cell<Option<Timestamp>>,
}

impl<'a, C: Clock> Cached<'a, C> {
#[inline]
pub fn new(clock: &'a C) -> Self {
Self {
clock,
cached_value: Default::default(),
}
}
}

impl<'a, C: Clock> Clock for Cached<'a, C> {
#[inline]
fn get_time(&self) -> Timestamp {
if let Some(time) = self.cached_value.get() {
return time;
}

let now = self.clock.get_time();
self.cached_value.set(Some(now));
now
}
}
13 changes: 13 additions & 0 deletions quic/s2n-quic-core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ impl<F: FnMut(&Timer) -> Result> Query for ForEach<F> {
}
}

#[cfg(feature = "tracing")]
pub struct Debugger;

#[cfg(feature = "tracing")]
impl Query for Debugger {
#[inline]
#[track_caller]
fn on_timer(&mut self, timer: &Timer) -> Result {
tracing::trace!(location = %core::panic::Location::caller(), timer = ?timer);
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit cc8d04f

Please sign in to comment.