use core::{any::Any, hash::Hash, time::Duration};
use std::{collections::HashSet, sync::Arc};
use jiff::SignedDuration;
use smol_bytes::Utf8Bytes;
use crate::{
BoxedError,
drive::{
Awaiting, Delivery, Evictor, Failure, Fate, LifecycleTap, NodeName, Observation, Outbox,
QueryError, Station, Taps,
},
};
use super::{
Concern, DeadlinePolicy, Missed, Occupancy, OverflowDecision, OverflowPolicy, RetryDecision,
RetryPolicy, ShedKey, TickDecision, TickPolicy,
};
#[cfg(test)]
mod tests;
fn advise<D>(
taps: &Taps,
node: &Utf8Bytes,
concern: Concern,
asked: Result<D, BoxedError>,
shipped: D,
) -> D {
match asked {
Ok(decision) => decision,
Err(error) => {
taps.observe(
NodeName::new(node),
&Observation::PolicyFailed { concern, error },
);
shipped
}
}
}
#[derive(Clone)]
pub struct Ticks {
policy: Arc<dyn TickPolicy>,
node: Utf8Bytes,
taps: Taps,
}
impl Ticks {
#[inline]
pub(super) const fn new(policy: Arc<dyn TickPolicy>, node: Utf8Bytes, taps: Taps) -> Self {
Self { policy, node, taps }
}
#[must_use]
pub fn on_missed(&self, missed: Missed) -> TickDecision {
advise(
&self.taps,
&self.node,
Concern::Tick,
self.policy.on_missed(missed),
TickDecision::Coalesce,
)
}
}
impl core::fmt::Debug for Ticks {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Ticks")
.field("node", &self.node.as_str())
.finish_non_exhaustive()
}
}
#[derive(Clone)]
pub struct Retries {
policy: Arc<dyn RetryPolicy>,
node: Utf8Bytes,
taps: Taps,
}
impl Retries {
#[inline]
pub(super) const fn new(policy: Arc<dyn RetryPolicy>, node: Utf8Bytes, taps: Taps) -> Self {
Self { policy, node, taps }
}
#[must_use]
pub fn settling(
&self,
station: Station,
attempt: u32,
failure: Option<&Failure>,
) -> RetryDecision {
if station.is_settled() {
return RetryDecision::GiveUp;
}
let decision = match failure {
Some(failure) => self.ask(attempt, failure.error()),
None => self.ask(attempt, &UnstatedFailure),
};
self.record(attempt, decision, failure.cloned());
decision
}
#[must_use]
pub fn on_failure(
&self,
attempt: u32,
error: &(dyn core::error::Error + 'static),
) -> RetryDecision {
let decision = self.ask(attempt, error);
self.record(attempt, decision, None);
decision
}
fn ask(&self, attempt: u32, error: &(dyn core::error::Error + 'static)) -> RetryDecision {
advise(
&self.taps,
&self.node,
Concern::Retry,
self.policy.on_failure(attempt, error),
RetryDecision::GiveUp,
)
}
fn record(&self, attempt: u32, decision: RetryDecision, failure: Option<Failure>) {
let observation = match decision {
RetryDecision::Retry { after } => Observation::Retrying { attempt, after },
RetryDecision::GiveUp => {
Fate::GivenUp {
attempt,
failure,
message: (),
}
.erase()
.0
}
};
self.taps.observe(NodeName::new(&self.node), &observation);
}
}
impl core::fmt::Debug for Retries {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Retries")
.field("node", &self.node.as_str())
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Display)]
#[display("the item failed and its error was not carried")]
struct UnstatedFailure;
impl core::error::Error for UnstatedFailure {}
#[derive(Debug)]
pub enum Handover<T, K> {
Sent,
Evicted(Delivery<T, K>),
Dropped(Delivery<T, K>),
Gone(Delivery<T, K>),
}
impl<T, K> Handover<T, K> {
#[inline]
#[must_use]
pub const fn is_sent(&self) -> bool {
matches!(self, Self::Sent | Self::Evicted(_))
}
#[inline]
#[must_use]
pub const fn is_shed(&self) -> bool {
matches!(self, Self::Evicted(_) | Self::Dropped(_))
}
}
#[derive(Clone)]
pub struct Sheds {
policy: Arc<dyn OverflowPolicy>,
node: Utf8Bytes,
taps: Taps,
}
impl Sheds {
#[inline]
pub(super) const fn new(policy: Arc<dyn OverflowPolicy>, node: Utf8Bytes, taps: Taps) -> Self {
Self { policy, node, taps }
}
#[must_use]
pub fn on_full(&self, occupancy: Occupancy) -> OverflowDecision {
advise(
&self.taps,
&self.node,
Concern::Overflow,
self.policy.on_full(occupancy),
OverflowDecision::Block,
)
}
pub fn shed<K>(&self, key: K, decision: OverflowDecision, occupancy: Occupancy)
where
K: Any + core::fmt::Display + Send + Sync,
{
self.taps.observe(
NodeName::new(&self.node),
&Observation::Shed {
key: ShedKey::new(key),
decision,
occupancy,
},
);
}
pub fn deliver<K, T>(
&self,
outbox: &Outbox<K, T>,
evictor: Option<&Evictor<K, T>>,
delivery: Delivery<T, K>,
) -> Handover<T, K>
where
K: Any + Clone + core::fmt::Display + Eq + Hash + Send + Sync,
{
let Err(delivery) = outbox.try_deliver(delivery) else {
return Handover::Sent;
};
if outbox.is_disconnected() {
return Handover::Gone(delivery);
}
let occupancy = occupancy(outbox);
match self.on_full(occupancy) {
OverflowDecision::Block => self.block(outbox, delivery),
OverflowDecision::DropNewest => self.drop_newest(delivery, occupancy),
OverflowDecision::DropOldest => match evictor.and_then(Evictor::evict) {
None => self.drop_newest(delivery, occupancy),
Some(oldest) => {
let oldest = self.shed_whole(oldest, OverflowDecision::DropOldest, occupancy);
match outbox.try_deliver(delivery) {
Ok(()) => Handover::Evicted(oldest),
Err(delivery) => match self.block(outbox, delivery) {
Handover::Sent => Handover::Evicted(oldest),
other => other,
},
}
}
},
}
}
fn drop_newest<K, T>(&self, delivery: Delivery<T, K>, occupancy: Occupancy) -> Handover<T, K>
where
K: Any + Clone + core::fmt::Display + Eq + Hash + Send + Sync,
{
Handover::Dropped(self.shed_whole(delivery, OverflowDecision::DropNewest, occupancy))
}
fn shed_whole<K, T>(
&self,
delivery: Delivery<T, K>,
decision: OverflowDecision,
occupancy: Occupancy,
) -> Delivery<T, K>
where
K: Any + Clone + core::fmt::Display + Eq + Hash + Send + Sync,
{
self.mourn(&delivery, decision, occupancy);
let (payload, custody) = delivery.into_parts();
if let Some(custody) = custody
&& let Some(first) = payload.keys().next().cloned()
{
let credited: HashSet<K> = payload.keys().cloned().collect();
custody.fate(
credited,
self.node.clone(),
Fate::Shed {
key: ShedKey::new(first),
decision,
occupancy,
message: (),
},
);
}
Delivery::in_custody(payload)
}
fn block<K, T>(&self, outbox: &Outbox<K, T>, delivery: Delivery<T, K>) -> Handover<T, K> {
match outbox.deliver(delivery) {
Ok(()) => Handover::Sent,
Err(delivery) => Handover::Gone(delivery),
}
}
fn mourn<K, T>(&self, delivery: &Delivery<T, K>, decision: OverflowDecision, occupancy: Occupancy)
where
K: Any + Clone + core::fmt::Display + Send + Sync,
{
for key in delivery.payload().keys() {
self.shed(key.clone(), decision, occupancy);
}
}
}
impl core::fmt::Debug for Sheds {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Sheds")
.field("node", &self.node.as_str())
.finish_non_exhaustive()
}
}
fn occupancy<K, T>(outbox: &Outbox<K, T>) -> Occupancy {
match outbox.capacity() {
Some(capacity) => Occupancy::bounded(outbox.len(), capacity),
None => Occupancy::unbounded(outbox.len()),
}
}
#[derive(Clone)]
pub struct Deadlines {
policy: Arc<dyn DeadlinePolicy>,
node: Utf8Bytes,
taps: Taps,
}
impl Deadlines {
#[inline]
pub(super) const fn new(policy: Arc<dyn DeadlinePolicy>, node: Utf8Bytes, taps: Taps) -> Self {
Self { policy, node, taps }
}
#[must_use]
pub fn resolve(&self, explicit: Option<Duration>) -> Option<Duration> {
if explicit.is_some() {
return explicit;
}
advise(
&self.taps,
&self.node,
Concern::Deadline,
self.policy.deadline(),
None,
)
.map(unsigned)
}
pub fn take<R>(
&self,
awaiting: Awaiting<R>,
explicit: Option<Duration>,
) -> Result<R, QueryError> {
let Some(budget) = self.resolve(explicit) else {
return awaiting.take();
};
let taken = awaiting.take_within(budget);
if matches!(taken, Err(QueryError::TimedOut)) {
self.expired(budget);
}
taken
}
fn expired(&self, budget: Duration) {
let observation = Fate::Expired {
budget: SignedDuration::try_from(budget).unwrap_or(SignedDuration::MAX),
message: (),
}
.erase()
.0;
self.taps.observe(NodeName::new(&self.node), &observation);
}
}
impl core::fmt::Debug for Deadlines {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Deadlines")
.field("node", &self.node.as_str())
.finish_non_exhaustive()
}
}
#[inline]
fn unsigned(budget: SignedDuration) -> Duration {
if budget.is_negative() {
return Duration::ZERO;
}
budget.unsigned_abs()
}