use std::{future::Future, pin::Pin, time::Duration};
use log::warn;
use super::{ConnectionActor, UnobservedCause};
use crate::{
app::Packet,
correlation::CorrelatableFrame,
metrics::ResponseCancellationReason,
push::FrameLike,
};
pub(crate) type GraceFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
pub(crate) trait GraceTimer: Send + Sync {
fn elapse(&self, grace: Duration) -> GraceFuture;
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct TokioGraceTimer;
impl GraceTimer for TokioGraceTimer {
fn elapse(&self, grace: Duration) -> GraceFuture { Box::pin(tokio::time::sleep(grace)) }
}
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
pub(super) fn cancel_stalled_response(&mut self) {
warn!(
"the peer could not be observed ({}) and accepted no frame for {:?}; cancelling: \
id={:?}, peer={:?}, correlation={:?}",
self.unobserved_cause_label(),
self.peer_closed_grace,
self.connection_id,
self.peer_addr,
self.response_correlation
);
ResponseCancellationReason::IdleGraceExpired.record();
self.active_output.clear();
}
fn unobserved_cause_label(&self) -> &'static str {
self.unobserved_cause
.as_ref()
.and_then(|cause| cause.get().copied())
.map_or("cause unknown", UnobservedCause::as_str)
}
}