Fix time issues on Wasm (#2544)
This commit is contained in:
parent
e2cc94b2ad
commit
d06ed3ad3a
3 changed files with 21 additions and 7 deletions
|
@ -29,8 +29,12 @@ use std::collections::HashMap;
|
|||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tracing::instrument;
|
||||
use tracing::trace;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Used for cluster logic to move LQ data to LQ cleanup code
|
||||
/// Not a stored struct; Used only in this module
|
||||
|
@ -552,9 +556,9 @@ impl Datastore {
|
|||
// tick is called periodically to perform maintenance tasks.
|
||||
// This is called every TICK_INTERVAL.
|
||||
pub async fn tick(&self) -> Result<(), Error> {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_err(|e| Error::Internal(e.to_string()))?;
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH).map_err(|e| {
|
||||
Error::Internal(format!("Clock may have gone backwards: {:?}", e.duration()))
|
||||
})?;
|
||||
let ts = now.as_secs();
|
||||
self.tick_at(ts).await?;
|
||||
Ok(())
|
||||
|
|
|
@ -42,8 +42,11 @@ use std::fmt;
|
|||
use std::fmt::Debug;
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use uuid::Uuid;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// A set of undoable updates and requests against a dataset.
|
||||
#[allow(dead_code)]
|
||||
|
@ -1002,7 +1005,10 @@ impl Transaction {
|
|||
|
||||
pub(crate) fn clock(&self) -> Timestamp {
|
||||
// Use a timestamp oracle if available
|
||||
let now: u128 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
|
||||
let now: u128 = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||
Ok(duration) => duration.as_millis(),
|
||||
Err(error) => panic!("Clock may have gone backwards: {:?}", error.duration()),
|
||||
};
|
||||
Timestamp {
|
||||
value: now as u64,
|
||||
}
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Mutex;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasmtimer::std::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use super::{u16_u64_to_versionstamp, u64_to_versionstamp, u64_u16_to_versionstamp, Versionstamp};
|
||||
|
||||
|
@ -144,9 +147,10 @@ fn now() -> Versionstamp {
|
|||
#[allow(unused)]
|
||||
// Returns the number of seconds since the Unix Epoch (January 1st, 1970 at UTC).
|
||||
fn secs_since_unix_epoch() -> u64 {
|
||||
let start = SystemTime::now();
|
||||
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
since_the_epoch.as_secs()
|
||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||
Ok(duration) => duration.as_secs(),
|
||||
Err(error) => panic!("Clock may have gone backwards: {:?}", error.duration()),
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
|
|
Loading…
Reference in a new issue