use std::time::Duration;
use tracing::{debug, warn};
use super::stream_active::BodyIdentity;
use crate::{
message_assembler::MessageKey,
metrics::StreamingBodyOutcome,
request::{SendChunkError, SendClosed},
};
const ABANDONED_BODY: &str = "abandoned_body";
const CLOSED: &str = "closed";
const ADMISSION_TIMEOUT: &str = StreamingBodyOutcome::AdmissionTimeout.as_str();
const DELIVERY_TIMEOUT: &str = StreamingBodyOutcome::DeliveryTimeout.as_str();
const NO_HANDLER: &str = StreamingBodyOutcome::NoHandler.as_str();
const RECEIVER_GONE: &str = "receiver_gone";
const REPLY_ABANDONED: &str = "reply_abandoned";
const CHUNK_DELIVERY: &str = "chunk_delivery";
const PREFILLED_BODY: &str = "prefilled_body";
const TERMINAL_BODY: &str = "terminal_body";
const STREAM_START: &str = "stream_start";
pub(super) fn read_timed_out() {
debug!("read timeout elapsed; continuing to wait for next frame");
}
pub(super) fn terminal_delivery_timed_out(identity: BodyIdentity) {
warn!(
operation = TERMINAL_BODY,
message_id = identity.message_id,
correlation_id = ?identity.correlation_id,
error_category = DELIVERY_TIMEOUT,
"timed out delivering the request body failure"
);
}
#[derive(Clone, Copy, Debug)]
enum ClosedDelivery {
TerminalFailure,
FinalChunk,
}
impl ClosedDelivery {
const fn describe(self) -> (&'static str, &'static str, &'static str) {
match self {
Self::TerminalFailure => (
TERMINAL_BODY,
RECEIVER_GONE,
"request body receiver gone before the failure was delivered",
),
Self::FinalChunk => (
CHUNK_DELIVERY,
ABANDONED_BODY,
"final request-body chunk refused; the body is abandoned",
),
}
}
}
fn channel_closed(identity: BodyIdentity, delivery: ClosedDelivery, error: &dyn std::fmt::Debug) {
let (operation, category, message) = delivery.describe();
debug!(
operation,
message_id = identity.message_id,
correlation_id = ?identity.correlation_id,
error_category = category,
error = ?error,
"{message}"
);
}
pub(super) fn terminal_receiver_gone(identity: BodyIdentity, error: SendClosed) {
channel_closed(identity, ClosedDelivery::TerminalFailure, &error);
}
pub(super) fn prefilled_not_delivered(message_id: u32, error: SendChunkError) {
debug!(
operation = PREFILLED_BODY,
message_id,
error_category = CLOSED,
?error,
"pre-filled request body was not delivered"
);
}
pub(super) fn prefilled_not_admitted(message_id: u32) {
warn!(
operation = PREFILLED_BODY,
message_id,
error_category = ADMISSION_TIMEOUT,
"pre-filled request body was not admitted within the read timeout"
);
}
pub(super) fn prefilled_failure_not_delivered(message_id: u32, error: SendClosed) {
debug!(
operation = PREFILLED_BODY,
message_id,
error_category = RECEIVER_GONE,
?error,
"pre-filled request body failure was not delivered"
);
}
pub(super) fn no_streaming_handler(message_id: u32) {
warn!(
operation = STREAM_START,
message_id,
error_category = NO_HANDLER,
"no streaming handler for message id"
);
}
pub(super) fn abandoned_chunk(key: MessageKey) {
debug!(
operation = CHUNK_DELIVERY,
?key,
error_category = ABANDONED_BODY,
"dropping chunk for abandoned streaming message"
);
}
pub(super) fn final_chunk_refused(identity: BodyIdentity, error: SendChunkError) {
channel_closed(identity, ClosedDelivery::FinalChunk, &error);
}
pub(super) fn no_live_handler(key: MessageKey) {
warn!(
operation = TERMINAL_BODY,
?key,
error_category = NO_HANDLER,
"streaming failure for a message with no live handler"
);
}
pub(super) fn admission_timed_out(identity: BodyIdentity, wait: Duration) {
report_admission(identity, wait, false);
}
pub(super) fn body_stalled(identity: BodyIdentity, elapsed: Duration) {
report_admission(identity, elapsed, true);
}
fn report_admission(identity: BodyIdentity, interval: Duration, is_stalled: bool) {
let millis = u64::try_from(interval.as_millis()).unwrap_or(u64::MAX);
let BodyIdentity {
message_id,
correlation_id,
} = identity;
if is_stalled {
warn!(
operation = CHUNK_DELIVERY,
message_id,
correlation_id = ?correlation_id,
error_category = ADMISSION_TIMEOUT,
elapsed_ms = millis,
"request body admission made no progress"
);
} else {
warn!(
operation = CHUNK_DELIVERY,
message_id,
correlation_id = ?correlation_id,
error_category = ADMISSION_TIMEOUT,
wait_ms = millis,
"request body chunk waited for admission without settling"
);
}
}
pub(super) fn final_chunk_timed_out(identity: BodyIdentity) {
warn!(
operation = CHUNK_DELIVERY,
message_id = identity.message_id,
correlation_id = ?identity.correlation_id,
error_category = ABANDONED_BODY,
"final request body chunk was not admitted before the deadline"
);
}
pub(super) fn handler_deadline_exceeded(route_id: u32) {
warn!(
operation = TERMINAL_BODY,
message_id = route_id,
error_category = REPLY_ABANDONED,
"streaming handler exceeded the connection wait; its reply is abandoned"
);
}
#[cfg(test)]
#[path = "stream_diagnostics_tests.rs"]
mod tests;