Skip to main content

bitwarden_shared_unlock/
timing.rs

1//! How often a peer syncs, and the clock its dates come from.
2
3use std::time::Duration;
4
5use crate::{PEER_STALE_AFTER, SYNC_INTERVAL, VAULT_TIMEOUT_GRACE_PERIOD};
6
7/// How often a peer syncs, and how long it tolerates silence from another peer.
8///
9/// Separated out from the constants so tests can run the protocol on millisecond timings rather
10/// than waiting out the multi-second production ones. Not part of the public API: every real client
11/// uses [`SharedUnlockTiming::default`].
12#[derive(Clone, Copy, Debug)]
13pub(crate) struct SharedUnlockTiming {
14    pub(crate) sync_interval: Duration,
15    pub(crate) vault_timeout_grace_period: Duration,
16    pub(crate) peer_stale_after: Duration,
17}
18
19impl Default for SharedUnlockTiming {
20    fn default() -> Self {
21        Self {
22            sync_interval: SYNC_INTERVAL,
23            vault_timeout_grace_period: VAULT_TIMEOUT_GRACE_PERIOD,
24            peer_stale_after: PEER_STALE_AFTER,
25        }
26    }
27}
28
29/// Milliseconds since the Unix epoch. `web_time` re-exports `std::time` off wasm, so this is the
30/// same clock on every target.
31pub(crate) fn now_millis() -> u64 {
32    web_time::SystemTime::now()
33        .duration_since(web_time::UNIX_EPOCH)
34        .unwrap_or_default()
35        .as_millis() as u64
36}