Delivery time leaves out the planned hold on aggregate reports (#517) #518

Merged
jcoffey-dev merged 1 commits from fix/517-report-hold-delivery-time into main 2026-11-21 00:54:48 +00:00
3 changed files with 107 additions and 3 deletions

No files matched your search

+7 -3
View File
@@ -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
+96
View File
@@ -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);
}
}
+4
View File
@@ -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;