use std::any::Any;
use std::fmt;
use std::future::ready;
use std::hash::{BuildHasher, RandomState};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use crate::kernel::{Ctx, Effect, fail, sleep, succeed, suspend};
pub struct ScheduleState(Box<dyn Any + Send>);
impl ScheduleState {
fn new<S: Send + 'static>(s: S) -> Self {
Self(Box::new(s))
}
fn take<S: 'static>(self) -> S {
match self.0.downcast::<S>() {
Ok(s) => *s,
Err(_) => panic!("ScheduleState stepped by a schedule that did not create it"),
}
}
}
impl fmt::Debug for ScheduleState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("ScheduleState(..)")
}
}
#[derive(Debug)]
pub struct ScheduleStep<Out> {
state: ScheduleState,
out: Out,
delay: Option<Duration>,
}
impl<Out> ScheduleStep<Out> {
pub fn out(&self) -> &Out {
&self.out
}
pub fn delay(&self) -> Option<Duration> {
self.delay
}
pub fn is_done(&self) -> bool {
self.delay.is_none()
}
pub fn into_parts(self) -> (ScheduleState, Out, Option<Duration>) {
(self.state, self.out, self.delay)
}
}
trait Driver<Out, In>: Send + Sync {
fn initial(&self) -> ScheduleState;
fn step(&self, now: Instant, input: &In, state: ScheduleState) -> ScheduleStep<Out>;
}
struct FromFn<I, F, S> {
init: I,
step: F,
_state: PhantomData<fn() -> S>,
}
impl<Out, In, S, I, F> Driver<Out, In> for FromFn<I, F, S>
where
S: Send + 'static,
I: Fn() -> S + Send + Sync,
F: Fn(Instant, &In, S) -> (S, Out, Option<Duration>) + Send + Sync,
{
fn initial(&self) -> ScheduleState {
ScheduleState::new((self.init)())
}
fn step(&self, now: Instant, input: &In, state: ScheduleState) -> ScheduleStep<Out> {
let (state, out, delay) = (self.step)(now, input, state.take::<S>());
ScheduleStep {
state: ScheduleState::new(state),
out,
delay,
}
}
}
pub struct Schedule<Out, In> {
driver: Arc<dyn Driver<Out, In>>,
clock: bool,
}
impl<Out, In> Clone for Schedule<Out, In> {
fn clone(&self) -> Self {
Self {
driver: Arc::clone(&self.driver),
clock: self.clock,
}
}
}
impl<Out, In> fmt::Debug for Schedule<Out, In> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Schedule(..)")
}
}
impl<Out: 'static, In: 'static> Schedule<Out, In> {
pub fn from_fn<S, I, F>(init: I, step: F) -> Self
where
S: Send + 'static,
I: Fn() -> S + Send + Sync + 'static,
F: Fn(Instant, &In, S) -> (S, Out, Option<Duration>) + Send + Sync + 'static,
{
Self::with_clock(true, init, step)
}
fn with_clock<S, I, F>(clock: bool, init: I, step: F) -> Self
where
S: Send + 'static,
I: Fn() -> S + Send + Sync + 'static,
F: Fn(Instant, &In, S) -> (S, Out, Option<Duration>) + Send + Sync + 'static,
{
Self {
driver: Arc::new(FromFn {
init,
step,
_state: PhantomData,
}),
clock,
}
}
pub fn start(&self) -> ScheduleState {
self.driver.initial()
}
pub fn step(&self, now: Instant, input: &In, state: ScheduleState) -> ScheduleStep<Out> {
self.driver.step(now, input, state)
}
pub fn map<O2: 'static>(self, f: impl Fn(Out) -> O2 + Send + Sync + 'static) -> Schedule<O2, In> {
let first = self.clone();
Schedule::with_clock(
self.clock,
move || first.start(),
move |now, input, s| {
let (s, out, delay) = self.step(now, input, s).into_parts();
(s, f(out), delay)
},
)
}
pub fn union<O2: 'static>(self, other: Schedule<O2, In>) -> Schedule<(Out, O2), In> {
self.zip_with(other, |a, b| match (a, b) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, None) => a,
(None, b) => b,
})
}
pub fn intersect<O2: 'static>(self, other: Schedule<O2, In>) -> Schedule<(Out, O2), In> {
self.zip_with(other, |a, b| match (a, b) {
(Some(a), Some(b)) => Some(a.max(b)),
_ => None,
})
}
fn zip_with<O2: 'static>(
self,
other: Schedule<O2, In>,
combine: fn(Option<Duration>, Option<Duration>) -> Option<Duration>,
) -> Schedule<(Out, O2), In> {
let (l, r) = (self.clone(), other.clone());
Schedule::with_clock(
self.clock || other.clock,
move || (l.start(), r.start()),
move |now, input, (sa, sb)| {
let (sa, a, da) = self.step(now, input, sa).into_parts();
let (sb, b, db) = other.step(now, input, sb).into_parts();
((sa, sb), (a, b), combine(da, db))
},
)
}
pub fn and_then(self, other: Schedule<Out, In>) -> Self {
enum Phase {
First(ScheduleState),
Second(ScheduleState),
}
let first = self.clone();
Schedule::with_clock(
self.clock || other.clock,
move || Phase::First(first.start()),
move |now, input, phase| {
if let Phase::First(s) = phase {
let (s, out, delay) = self.step(now, input, s).into_parts();
if delay.is_some() {
return (Phase::First(s), out, delay);
}
let (s, out, delay) = other.step(now, input, other.start()).into_parts();
return (Phase::Second(s), out, delay);
}
let Phase::Second(s) = phase else {
unreachable!("the first phase returned above")
};
let (s, out, delay) = other.step(now, input, s).into_parts();
(Phase::Second(s), out, delay)
},
)
}
pub fn while_input(self, pred: impl Fn(&In) -> bool + Send + Sync + 'static) -> Self {
let first = self.clone();
Schedule::with_clock(
self.clock,
move || first.start(),
move |now, input, s| {
let (s, out, delay) = self.step(now, input, s).into_parts();
(s, out, delay.filter(|_| pred(input)))
},
)
}
pub fn up_to(self, max: Duration) -> Self {
let first = self.clone();
Schedule::from_fn(
move || (first.start(), None::<Instant>),
move |now, input, (s, start)| {
let start = start.unwrap_or(now);
let (s, out, delay) = self.step(now, input, s).into_parts();
let delay = delay.filter(|_| now.saturating_duration_since(start) < max);
((s, Some(start)), out, delay)
},
)
}
pub fn jittered(self) -> Self {
self.jittered_with(0.8, 1.2, JitterSource::entropy())
}
pub fn jittered_with(self, min: f64, max: f64, source: JitterSource) -> Self {
let first = self.clone();
Schedule::with_clock(
self.clock,
move || (first.start(), (source.seed)()),
move |now, input, (s, mut rng)| {
let (s, out, delay) = self.step(now, input, s).into_parts();
let delay = delay.map(|d| {
let factor = min + (max - min) * unit_f64(splitmix64(&mut rng));
scale(d, factor)
});
((s, rng), out, delay)
},
)
}
}
impl<In: 'static> Schedule<u64, In> {
pub fn forever() -> Self {
Self::with_clock(
false,
|| 0u64,
|_, _, n| (n.saturating_add(1), n, Some(Duration::ZERO)),
)
}
pub fn recurs(times: u64) -> Self {
Self::with_clock(
false,
|| 0u64,
move |_, _, n| {
(
n.saturating_add(1),
n,
(n < times).then_some(Duration::ZERO),
)
},
)
}
pub fn spaced(interval: Duration) -> Self {
Self::with_clock(
false,
|| 0u64,
move |_, _, n| (n.saturating_add(1), n, Some(interval)),
)
}
}
impl<In: 'static> Schedule<Duration, In> {
pub fn exponential(base: Duration) -> Self {
Self::with_clock(
false,
|| 0u32,
move |_, _, k| {
let d = 1u32
.checked_shl(k)
.map_or(Duration::MAX, |m| base.saturating_mul(m));
(k.saturating_add(1), d, Some(d))
},
)
}
pub fn exponential_by(base: Duration, factor: f64) -> Self {
Self::with_clock(
false,
|| 0i32,
move |_, _, k| {
let d = scale(base, factor.powi(k));
(k.saturating_add(1), d, Some(d))
},
)
}
pub fn fibonacci(one: Duration) -> Self {
Self::with_clock(
false,
move || (one, one),
|_, _, (a, b)| ((b, a.saturating_add(b)), a, Some(a)),
)
}
}
fn scale(d: Duration, factor: f64) -> Duration {
if factor.is_nan() || factor <= 0.0 {
return Duration::ZERO;
}
Duration::try_from_secs_f64(d.as_secs_f64() * factor).unwrap_or(Duration::MAX)
}
#[derive(Clone)]
pub struct JitterSource {
seed: Arc<dyn Fn() -> u64 + Send + Sync>,
}
impl fmt::Debug for JitterSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("JitterSource(..)")
}
}
impl JitterSource {
pub fn entropy() -> Self {
static COUNTER: AtomicU64 = AtomicU64::new(0);
Self::from_fn(|| RandomState::new().hash_one(COUNTER.fetch_add(1, Ordering::Relaxed)))
}
pub fn seeded(seed: u64) -> Self {
Self::from_fn(move || seed)
}
pub fn from_fn(f: impl Fn() -> u64 + Send + Sync + 'static) -> Self {
Self { seed: Arc::new(f) }
}
}
fn splitmix64(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn unit_f64(x: u64) -> f64 {
(x >> 11) as f64 / (1u64 << 53) as f64
}
fn clock_now<E: Send + 'static, R: Send + Sync + 'static>() -> Effect<Instant, E, R> {
Effect::from_ctx(|ctx: Ctx<R>| ready(Ok(ctx.fiber().executor().now())))
}
fn with_now<A, E, R>(
clock: bool,
f: impl FnOnce(Instant) -> Effect<A, E, R> + Send + 'static,
) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
static UNREAD: OnceLock<Instant> = OnceLock::new();
if clock {
clock_now().flat_map(f)
} else {
f(*UNREAD.get_or_init(Instant::now))
}
}
fn after<A, E, R>(d: Duration, next: Effect<A, E, R>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
if d.is_zero() {
next
} else {
sleep(d).then(next)
}
}
fn retry_loop<A, E, E2, R, Out, F, G>(
make: Arc<F>,
schedule: Schedule<Out, E>,
state: ScheduleState,
or_else: G,
) -> Effect<A, E2, R>
where
A: Send + 'static,
E: Send + 'static,
E2: Send + 'static,
R: Send + Sync + 'static,
Out: Send + 'static,
F: Fn() -> Effect<A, E, R> + Send + Sync + 'static,
G: FnOnce(E, Out) -> Effect<A, E2, R> + Send + 'static,
{
suspend(move || {
make().catch(move |e| {
with_now(schedule.clock, move |now| {
let (state, out, delay) = schedule.step(now, &e, state).into_parts();
match delay {
Some(d) => after(d, retry_loop(make, schedule, state, or_else)),
None => or_else(e, out),
}
})
})
})
}
fn repeat_loop<A, E, E2, R, Out, F, G>(
make: Arc<F>,
schedule: Schedule<Out, A>,
state: ScheduleState,
last: Option<Out>,
or_else: G,
) -> Effect<Out, E2, R>
where
A: Send + 'static,
E: Send + 'static,
E2: Send + 'static,
R: Send + Sync + 'static,
Out: Send + 'static,
F: Fn() -> Effect<A, E, R> + Send + Sync + 'static,
G: FnOnce(E, Option<Out>) -> Effect<Out, E2, R> + Send + 'static,
{
suspend(move || {
make()
.map(Ok)
.catch(|e| succeed(Err(e)))
.flat_map(move |r: Result<A, E>| match r {
Err(e) => or_else(e, last),
Ok(a) => with_now(schedule.clock, move |now| {
let (state, out, delay) = schedule.step(now, &a, state).into_parts();
match delay {
Some(d) => after(d, repeat_loop(make, schedule, state, Some(out), or_else)),
None => succeed(out),
}
}),
})
})
}
impl<A, E, R> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn retry<Out: Send + 'static>(
make: impl Fn() -> Self + Send + Sync + 'static,
schedule: Schedule<Out, E>,
) -> Self {
Self::retry_or_else(make, schedule, |e, _| fail(e))
}
pub fn retry_or_else<Out: Send + 'static, E2: Send + 'static>(
make: impl Fn() -> Self + Send + Sync + 'static,
schedule: Schedule<Out, E>,
or_else: impl FnOnce(E, Out) -> Effect<A, E2, R> + Send + 'static,
) -> Effect<A, E2, R> {
let make = Arc::new(make);
suspend(move || {
let state = schedule.start();
retry_loop(make, schedule, state, or_else)
})
}
pub fn repeat<Out: Send + 'static>(
make: impl Fn() -> Self + Send + Sync + 'static,
schedule: Schedule<Out, A>,
) -> Effect<Out, E, R> {
Self::repeat_or_else(make, schedule, |e, _| fail(e))
}
pub fn repeat_or_else<Out: Send + 'static, E2: Send + 'static>(
make: impl Fn() -> Self + Send + Sync + 'static,
schedule: Schedule<Out, A>,
or_else: impl FnOnce(E, Option<Out>) -> Effect<Out, E2, R> + Send + 'static,
) -> Effect<Out, E2, R> {
let make = Arc::new(make);
suspend(move || {
let state = schedule.start();
repeat_loop(make, schedule, state, None, or_else)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn delays<Out: 'static>(s: &Schedule<Out, ()>, n: usize) -> Vec<Option<Duration>> {
let now = Instant::now();
let mut state = s.start();
let mut out = Vec::new();
for _ in 0..n {
let (next, _, d) = s.step(now, &(), state).into_parts();
state = next;
out.push(d);
}
out
}
fn ms(n: u64) -> Option<Duration> {
Some(Duration::from_millis(n))
}
#[test]
fn exponential_saturates() {
let s = Schedule::<Duration, ()>::exponential(Duration::from_secs(1));
assert_eq!(delays(&s, 40)[39], Some(Duration::MAX));
}
#[test]
fn exponential_by_and_scale_edges() {
let s = Schedule::<Duration, ()>::exponential_by(Duration::from_millis(10), 3.0);
assert_eq!(delays(&s, 3), vec![ms(10), ms(30), ms(90)]);
assert_eq!(scale(Duration::from_secs(1), -1.0), Duration::ZERO);
assert_eq!(scale(Duration::from_secs(1), f64::NAN), Duration::ZERO);
assert_eq!(scale(Duration::MAX, 1e300), Duration::MAX);
}
#[test]
#[should_panic(expected = "did not create it")]
fn foreign_state_panics() {
let a = Schedule::<u64, ()>::forever();
let b = Schedule::<Duration, ()>::fibonacci(Duration::from_millis(1));
let _ = a.step(Instant::now(), &(), b.start());
}
#[test]
fn debug_impls() {
let s = Schedule::<u64, ()>::forever();
assert_eq!(format!("{s:?}"), "Schedule(..)");
assert_eq!(format!("{:?}", s.start()), "ScheduleState(..)");
assert_eq!(format!("{:?}", JitterSource::seeded(1)), "JitterSource(..)");
}
#[test]
fn unit_f64_is_in_range() {
assert_eq!(unit_f64(0), 0.0);
assert!(unit_f64(u64::MAX) < 1.0);
}
}