From 68f3d299796e9bac993952f1c8eb5fdeb0f10346 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Fri, 20 Nov 2026 16:26:31 -0700 Subject: [PATCH] Delivery time leaves out the planned hold on aggregate reports (#517) Aggregate DMARC and TLS reports are queued with their first attempt at a random time up to three hours later, so reports from every server don't reach receivers at once. Delivery time was counted from when a message was queued, so a report held for hours on purpose and sent on its first try showed up as hours of delivery time: the console's delivery-time graph spiked and its average read 18 minutes against 2 seconds per attempt. send_report now records the hold in the message's flags, in the top 14 bits (50-63), which no flag uses, and the delivery-completed event's elapsed time counts from the end of the hold. That event feeds the delivery-time histogram, the stored metric the dashboard and scheduled reports read, and delivery history. The hold itself is unchanged. Spare bits rather than a new field, so the stored queue format doesn't change: a node that doesn't know them ignores them and measures as before, and the console's flag list only reads the bits it knows. Messages already queued carry no hold and are measured as before. Reported on Discord (#inbuxa). Fixes #517 --- crates/smtp/src/outbound/delivery.rs | 10 ++- crates/smtp/src/queue/mod.rs | 96 ++++++++++++++++++++++++++++ crates/smtp/src/reporting/send.rs | 4 ++ 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/crates/smtp/src/outbound/delivery.rs b/crates/smtp/src/outbound/delivery.rs index 6fb6b48f..1d775e21 100644 --- a/crates/smtp/src/outbound/delivery.rs +++ b/crates/smtp/src/outbound/delivery.rs @@ -22,7 +22,7 @@ use crate::queue::spool::{DSN_RETRY, SmtpSpool}; use crate::queue::throttle::IsAllowed; use crate::queue::{ Error, FROM_REPORT, HostResponse, MessageWrapper, Metadata, QueueEnvelope, QueuedMessage, - RCPT_RELAYED, Status, + RCPT_RELAYED, Status, delivery_time, }; use crate::reporting::send::MtaReportSend; use crate::{queue::ErrorDetails, reporting::tls::TlsRptOptions}; @@ -192,7 +192,9 @@ impl QueuedMessage { trc::event!( Delivery(DeliveryEvent::Completed), SpanId = span_id, - Elapsed = trc::Value::Duration((now() - message.message.created) * 1000) + Elapsed = trc::Value::Duration( + delivery_time(message.message.created, message.message.flags, now()) * 1000 + ) ); // All message recipients expired, do not re-queue. @@ -1636,7 +1638,9 @@ impl QueuedMessage { trc::event!( Delivery(DeliveryEvent::Completed), SpanId = span_id, - Elapsed = trc::Value::Duration((now() - message.message.created) * 1000) + Elapsed = trc::Value::Duration( + delivery_time(message.message.created, message.message.flags, now()) * 1000 + ) ); // Delete message from queue diff --git a/crates/smtp/src/queue/mod.rs b/crates/smtp/src/queue/mod.rs index a6eae2b9..34afb8f6 100644 --- a/crates/smtp/src/queue/mod.rs +++ b/crates/smtp/src/queue/mod.rs @@ -175,6 +175,38 @@ pub const FROM_DSN: u64 = 1 << 35; pub const FROM_REPORT: u64 = 1 << 36; pub const FROM_AUTOGENERATED: u64 = 1 << 37; +// inbuxa (#517): a scheduled report's planned hold before its first attempt, +// in seconds, kept in the top 14 bits of the message flags. Delivery time +// counts from the end of the hold: an aggregate report held for hours on +// purpose and sent on its first try took seconds to deliver, and the +// delivery-time figures said hours. Spare bits rather than a new field, so +// the stored queue format doesn't change: a node that doesn't know them +// ignores them, and measures from when the message was queued, as before. +pub const FROM_HOLD_SHIFT: u64 = 50; +pub const FROM_HOLD_MAX: u64 = (1 << 14) - 1; +pub const FROM_HOLD_MASK: u64 = FROM_HOLD_MAX << FROM_HOLD_SHIFT; + +/// `flags` with the planned hold set to `seconds` (at most `FROM_HOLD_MAX`). +pub const fn with_planned_hold(flags: u64, seconds: u64) -> u64 { + let seconds = if seconds > FROM_HOLD_MAX { + FROM_HOLD_MAX + } else { + seconds + }; + (flags & !FROM_HOLD_MASK) | (seconds << FROM_HOLD_SHIFT) +} + +/// The planned hold recorded in a message's flags, in seconds. +pub const fn planned_hold(flags: u64) -> u64 { + (flags & FROM_HOLD_MASK) >> FROM_HOLD_SHIFT +} + +/// Seconds from when a message was due its first attempt to `now`: when it +/// was queued, plus any planned hold. +pub const fn delivery_time(created: u64, flags: u64, now: u64) -> u64 { + now.saturating_sub(created + planned_hold(flags)) +} + pub const RCPT_DSN_SENT: u64 = 1 << 32; // inbuxa: handed to an inbuxa Relay, which hasn't reported a final status // (relay spec, RL-24) @@ -789,3 +821,67 @@ mod subject_tests { assert!(subject.ends_with('…')); } } + +#[cfg(test)] +mod planned_hold_tests { + use super::{ + FROM_AUTHENTICATED, FROM_AUTOGENERATED, FROM_DSN, FROM_HOLD_MASK, FROM_HOLD_MAX, + FROM_REPORT, FROM_UNAUTHENTICATED, FROM_UNAUTHENTICATED_DMARC, delivery_time, planned_hold, + with_planned_hold, + }; + use crate::queue::journal::FROM_JOURNAL; + use smtp_proto::{ + MAIL_BODY_8BITMIME, MAIL_BODY_BINARYMIME, MAIL_BY_NOTIFY, MAIL_BY_RETURN, MAIL_BY_TRACE, + MAIL_CONPERM, MAIL_REQUIRETLS, MAIL_RET_FULL, MAIL_RET_HDRS, MAIL_SMTPUTF8, + }; + + #[test] + fn hold_bits_are_their_own() { + let others = FROM_AUTHENTICATED + | FROM_UNAUTHENTICATED + | FROM_UNAUTHENTICATED_DMARC + | FROM_DSN + | FROM_REPORT + | FROM_AUTOGENERATED + | FROM_JOURNAL + | MAIL_BODY_8BITMIME + | MAIL_BODY_BINARYMIME + | MAIL_BY_NOTIFY + | MAIL_BY_RETURN + | MAIL_BY_TRACE + | MAIL_CONPERM + | MAIL_REQUIRETLS + | MAIL_RET_FULL + | MAIL_RET_HDRS + | MAIL_SMTPUTF8; + assert_eq!(FROM_HOLD_MASK & others, 0); + // The longest hold a report gets (just under three hours) fits + assert!(FROM_HOLD_MAX >= 10_800); + } + + #[test] + fn hold_is_recorded_without_touching_other_flags() { + let flags = FROM_REPORT | MAIL_REQUIRETLS; + let held = with_planned_hold(flags, 9_000); + assert_eq!(planned_hold(held), 9_000); + assert_eq!(held & !FROM_HOLD_MASK, flags); + assert_eq!(planned_hold(flags), 0); + // Set again, not added to; too long is cut to the most it can hold + assert_eq!(planned_hold(with_planned_hold(held, 60)), 60); + assert_eq!( + planned_hold(with_planned_hold(flags, u64::MAX)), + FROM_HOLD_MAX + ); + } + + #[test] + fn delivery_time_counts_from_the_end_of_the_hold() { + // Queued at 1000, held 9000 s, sent on its first try 2 s after that + let flags = with_planned_hold(FROM_REPORT, 9_000); + assert_eq!(delivery_time(1_000, flags, 10_002), 2); + // A message with no hold is measured from when it was queued + assert_eq!(delivery_time(1_000, FROM_AUTHENTICATED, 1_300), 300); + // Sent early (Retry All in the console): not below zero + assert_eq!(delivery_time(1_000, flags, 5_000), 0); + } +} diff --git a/crates/smtp/src/reporting/send.rs b/crates/smtp/src/reporting/send.rs index d0b789b2..a73475ea 100644 --- a/crates/smtp/src/reporting/send.rs +++ b/crates/smtp/src/reporting/send.rs @@ -86,10 +86,14 @@ impl MtaReportSend for Server { if !deliver_now { #[cfg(not(feature = "test_mode"))] { + use crate::queue::with_planned_hold; use common::config::smtp::queue::QueueExpiry; use rand::RngExt; let delivery_time = rand::rng().random_range(0u64..10800u64); + // inbuxa (#517): recorded, so delivery time counts from the + // end of the hold rather than from now + message.message.flags = with_planned_hold(message.message.flags, delivery_time); for rcpt in &mut message.message.recipients { rcpt.retry.due += delivery_time; rcpt.notify.due += delivery_time; -- 2.54.0