use std::future::Future;
use std::pin::Pin;
use std::time::{Duration, SystemTime};
use chrono::{DateTime, SecondsFormat, Utc};
use tea_protocol::ProtocolTimestamp;
use crate::{KernelError, KernelErrorCode};
pub type KernelDeadlineFuture<'a> = Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
pub trait KernelClock: std::fmt::Debug + Send + Sync {
fn now(&self) -> Result<ProtocolTimestamp, KernelError>;
fn sleep_until(&self, deadline: ProtocolTimestamp) -> KernelDeadlineFuture<'_>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct TokioKernelClock;
impl KernelClock for TokioKernelClock {
fn now(&self) -> Result<ProtocolTimestamp, KernelError> {
canonical_system_time(SystemTime::now())
}
fn sleep_until(&self, deadline: ProtocolTimestamp) -> KernelDeadlineFuture<'_> {
let duration = self
.now()
.ok()
.and_then(|now| (deadline.as_utc() - now.as_utc()).to_std().ok())
.unwrap_or(Duration::ZERO);
Box::pin(tokio::time::sleep(duration))
}
}
fn canonical_system_time(time: SystemTime) -> Result<ProtocolTimestamp, KernelError> {
let value = DateTime::<Utc>::from(time).to_rfc3339_opts(SecondsFormat::Millis, true);
value.parse().map_err(|_| {
KernelError::new(
KernelErrorCode::ClockFailure,
"system clock did not produce a canonical timestamp",
)
})
}