use serde::{Deserialize, Serialize};
use tilo_clock::time::{Duration, Instant};
use super::{
ActionProvider, Caller, Completer, InvocationReceipt, InvocationState, Pending, PendingPoll,
ReceiptStatus, pending,
};
use crate::outcome::{InvocationId, InvocationUpdate};
pub const MAX_REMOTE_WAIT: Duration = Duration::from_secs(10);
const MAX_IMMEDIATE_WAKES: u32 = 64;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum UpdateMark {
Accepted,
Progress {
sequence: u64,
},
Item {
sequence: u64,
},
Finished,
}
impl UpdateMark {
#[must_use]
pub const fn of(update: &InvocationUpdate) -> Self {
match update {
InvocationUpdate::Accepted { .. } => Self::Accepted,
InvocationUpdate::Progress { sequence, .. } => Self::Progress {
sequence: *sequence,
},
InvocationUpdate::Item { sequence, .. } => Self::Item {
sequence: *sequence,
},
InvocationUpdate::Succeeded { .. }
| InvocationUpdate::Failed { .. }
| InvocationUpdate::Cancelled { .. } => Self::Finished,
}
}
}
pub(super) fn deadline(wait_ms: u32) -> Instant {
let now = tilo_clock::now();
let wait = Duration::from_millis(u64::from(wait_ms)).min(MAX_REMOTE_WAIT);
now.checked_add(wait).unwrap_or(now)
}
pub fn settle_with<H: ActionProvider + Clone + Send + 'static>(
provider: &H,
caller: &Caller,
invocation: InvocationId,
seen: Option<UpdateMark>,
until: Instant,
) -> Pending<Option<InvocationState>> {
let (answer, settled) = pending();
look(Wait {
provider: provider.clone(),
caller: caller.clone(),
invocation,
seen,
until,
answer,
});
settled
}
pub(super) fn settle_receipt<H: ActionProvider + Clone + Send + 'static>(
provider: &H,
caller: &Caller,
receipt: Pending<InvocationReceipt>,
until: Instant,
) -> Pending<InvocationReceipt> {
let provider = provider.clone();
let caller = caller.clone();
let (completer, settled) = pending();
receipt.then(move |receipt| {
let Some(receipt) = receipt else {
return;
};
if !matches!(receipt.status, ReceiptStatus::Accepted) {
completer.complete(receipt);
return;
}
settle_with(&provider, &caller, receipt.invocation, None, until).then(move |state| {
completer.complete(state.flatten().map_or(receipt, |state| state.receipt()));
});
});
settled
}
struct Wait<H> {
provider: H,
caller: Caller,
invocation: InvocationId,
seen: Option<UpdateMark>,
until: Instant,
answer: Completer<Option<InvocationState>>,
}
impl<H: ActionProvider> Wait<H> {
fn arrived(&self, state: Option<&InvocationState>) -> bool {
state.is_none_or(|state| {
state.is_terminal()
|| (self.seen.is_none() && state.read_answered)
|| self
.seen
.is_some_and(|seen| UpdateMark::of(&state.update) != seen)
})
}
fn over(&self) -> bool {
tilo_clock::now() >= self.until
|| self
.caller
.execution_lease()
.is_some_and(|lease| !lease.is_live())
}
}
fn look<H: ActionProvider + Send + 'static>(wait: Wait<H>) {
let mut wakes = 0_u32;
loop {
if wait.answer.is_abandoned() {
return;
}
let change = wait.provider.next_change(&wait.caller, &wait.invocation);
let state = wait.provider.observe(&wait.caller, &wait.invocation);
let answer_now =
wait.arrived(state.as_ref()) || wait.over() || wakes >= MAX_IMMEDIATE_WAKES;
let Some(mut change) = change.filter(|_| !answer_now) else {
wait.answer.complete(state);
return;
};
if matches!(change.poll_ready(), PendingPoll::Waiting) {
change.then(move |_| look(wait));
return;
}
wakes = wakes.saturating_add(1);
}
}
#[cfg(test)]
#[path = "wait_tests.rs"]
mod tests;