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 16, 2024
1 parent 311ece3 commit 108a74f
Showing 1 changed file with 36 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
}
}

0 comments on commit 108a74f

Please sign in to comment.