mailrs-outbound-queue 4.0.0

Outbound mail queue primitives: DKIM signing, DSN generation, MTA-STS lookup, retry/backoff, with a pluggable store trait and a Postgres reference implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Storage abstractions for the outbound queue.
//!
//! The bundled [`DeliveryWorker`](crate::DeliveryWorker) is currently
//! Postgres-only (enabled with the `pg` feature). This module defines the
//! traits a v1.x-compatible custom worker would target. External users who
//! don't want sqlx can disable the `pg` feature, implement [`QueueStore`]
//! and [`Notifier`] against their own backend (sqlite, sled, custom REST,
//! in-memory for tests), and reuse the pure delivery primitives in this
//! crate (`dkim_sign`, `dsn`, `mta_sts`, `retry`) plus
//! [`mailrs-smtp-client`](https://crates.io/crates/mailrs-smtp-client).
//!
//! Wider trait coverage and a generic worker are planned for a future minor
//! release.

use std::sync::Mutex;

use crate::queue::{QueueStatus, QueuedMessage};

/// Error returned by store operations. Backend-specific details are stringified
/// to keep the trait dyn-compatible across implementations.
#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    /// Backend-specific error (PG, SQLite, network — stringified).
    #[error("store backend: {0}")]
    Backend(String),
}

#[cfg(feature = "pg")]
impl From<sqlx::Error> for StoreError {
    fn from(err: sqlx::Error) -> Self {
        Self::Backend(err.to_string())
    }
}

/// The persistent store the outbound queue lives in.
///
/// `enqueue*` is called by the SMTP submission path. `dequeue` + `mark_*` is
/// called by a delivery worker. `list_recent` / `queue_stats` /
/// `cancel_*` / `retry_message` is called by admin / management code.
///
/// Implementations should be ACID-safe for the lifecycle transitions
/// `pending → inflight → {delivered, failed, bounced}` so a crashed worker
/// can't lose or duplicate-deliver a message.
#[async_trait::async_trait]
pub trait QueueStore: Send + Sync {
    /// Insert a new pending message. Returns the assigned id.
    #[allow(clippy::too_many_arguments)]
    async fn enqueue(
        &self,
        sender: &str,
        recipient: &str,
        domain: &str,
        message_data: &[u8],
        message_id: Option<&str>,
        now: i64,
        is_forwarded: bool,
    ) -> Result<i64, StoreError>;

    /// Insert a message scheduled to deliver at `scheduled_at`.
    #[allow(clippy::too_many_arguments)]
    async fn enqueue_scheduled(
        &self,
        sender: &str,
        recipient: &str,
        domain: &str,
        message_data: &[u8],
        message_id: Option<&str>,
        created_at: i64,
        scheduled_at: i64,
    ) -> Result<i64, StoreError>;

    /// Fetch up to `limit` pending messages whose `next_retry` is `<= now`.
    async fn dequeue(&self, now: i64, limit: u32) -> Result<Vec<QueuedMessage>, StoreError>;

    /// Reset `inflight` messages older than ~10 minutes back to `pending`
    /// (crash-recovery). Returns rows affected.
    async fn recover_stale_inflight(&self, now: i64) -> Result<u64, StoreError>;

    /// Mark a message as currently being delivered.
    async fn mark_inflight(&self, id: i64, now: i64) -> Result<(), StoreError>;

    /// Mark a message as delivered.
    async fn mark_delivered(&self, id: i64, now: i64) -> Result<(), StoreError>;

    /// Mark a delivery attempt as failed; the message goes back to `pending`
    /// with an incremented `attempts` and a `next_retry` of `next_retry`.
    async fn mark_failed(
        &self,
        id: i64,
        error: &str,
        next_retry: i64,
        now: i64,
    ) -> Result<(), StoreError>;

    /// Mark a message as permanently bounced (no more retries).
    async fn mark_bounced(&self, id: i64, error: &str, now: i64) -> Result<(), StoreError>;

    /// Fetch a single message by id, or `None` if it has been purged.
    async fn get_message(&self, id: i64) -> Result<Option<QueuedMessage>, StoreError>;

    /// Group counts by status — e.g. `[("pending", 12), ("inflight", 2)]`.
    async fn queue_stats(&self) -> Result<Vec<(String, i64)>, StoreError>;

    /// Recently-created entries, newest first, for admin UIs.
    async fn list_recent(&self, limit: i32) -> Result<Vec<QueuedMessage>, StoreError>;

    /// Delete a single `pending` message. Returns `true` if a row was removed.
    async fn cancel_pending(&self, id: i64) -> Result<bool, StoreError>;

    /// Delete a single `pending` message identified by RFC 5322 Message-ID and
    /// sender (so a user can only cancel their own undelivered messages).
    async fn cancel_pending_by_message_id(
        &self,
        message_id: &str,
        sender: &str,
    ) -> Result<bool, StoreError>;

    /// Reset a `bounced` / `failed` message to `pending` for another try.
    async fn retry_message(&self, id: i64, now: i64) -> Result<bool, StoreError>;

    /// `true` if the recipient is in the hard-bounce suppression list.
    async fn is_suppressed(&self, email: &str) -> bool;

    /// Add (or update) a recipient on the suppression list.
    async fn add_suppression(
        &self,
        email: &str,
        reason: &str,
        smtp_code: Option<i32>,
    ) -> Result<(), StoreError>;

    /// Remove a recipient from the suppression list. Returns `true` if a row
    /// was removed.
    async fn remove_suppression(&self, email: &str) -> Result<bool, StoreError>;

    /// `(email, reason, smtp_code, created_at_epoch)` tuples, newest first.
    async fn list_suppressions(
        &self,
        limit: i64,
    ) -> Result<Vec<(String, String, Option<i32>, i64)>, StoreError>;
}

/// Cross-process wake-up channel. The submitter calls [`notify`](Self::notify)
/// after `enqueue`; the worker awaits [`wait`](Self::wait) to short-circuit
/// its poll interval and pick up new work immediately.
///
/// Implementations may coalesce notifications: many `notify()` calls may
/// release a single `wait()`. The worker still re-polls the store on every
/// wake, so missing a notification is at worst a latency cost, never lost
/// work.
#[async_trait::async_trait]
pub trait Notifier: Send + Sync {
    /// Signal that new work was added.
    async fn notify(&self);

    /// Block until a notification arrives. Spurious wake-ups are allowed.
    async fn wait(&self);
}

/// In-process notifier backed by [`tokio::sync::Notify`].
///
/// Useful for tests, single-process deployments where the worker and
/// submitter share a runtime, or as a fallback when the cross-process
/// notifier is unavailable.
#[derive(Default)]
pub struct InMemoryNotifier {
    inner: tokio::sync::Notify,
}

impl InMemoryNotifier {
    /// Create a new notifier with no waiters.
    pub fn new() -> Self {
        Self::default()
    }
}

#[async_trait::async_trait]
impl Notifier for InMemoryNotifier {
    async fn notify(&self) {
        self.inner.notify_one();
    }
    async fn wait(&self) {
        self.inner.notified().await;
    }
}

/// Notifier that drops every signal. The worker still polls on its configured
/// interval, just without fast wake-ups. Useful when a real notifier hasn't
/// been wired up yet.
pub struct NoopNotifier;

#[async_trait::async_trait]
impl Notifier for NoopNotifier {
    async fn notify(&self) {}
    async fn wait(&self) {
        std::future::pending::<()>().await;
    }
}

/// Pure in-memory [`QueueStore`] for tests and single-process pilot
/// deployments. Not durable across restarts.
pub struct InMemoryQueueStore {
    state: Mutex<MemState>,
}

struct MemState {
    next_id: i64,
    messages: Vec<QueuedMessage>,
    suppressions: Vec<(String, String, Option<i32>, i64)>, // (email, reason, code, created_at)
}

impl Default for InMemoryQueueStore {
    fn default() -> Self {
        Self::new()
    }
}

impl InMemoryQueueStore {
    /// Create an empty in-memory store.
    pub fn new() -> Self {
        Self {
            state: Mutex::new(MemState {
                next_id: 1,
                messages: Vec::new(),
                suppressions: Vec::new(),
            }),
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn insert_msg(
        s: &mut MemState,
        sender: &str,
        recipient: &str,
        domain: &str,
        message_data: &[u8],
        message_id: Option<&str>,
        next_retry: i64,
        created_at: i64,
        is_forwarded: bool,
    ) -> i64 {
        let id = s.next_id;
        s.next_id += 1;
        s.messages.push(QueuedMessage {
            id,
            sender: sender.into(),
            recipient: recipient.into(),
            domain: domain.into(),
            message_data: message_data.to_vec(),
            status: QueueStatus::Pending,
            attempts: 0,
            max_attempts: 8,
            next_retry,
            last_error: None,
            message_id: message_id.map(str::to_owned),
            created_at,
            updated_at: created_at,
            is_forwarded,
        });
        id
    }
}

#[async_trait::async_trait]
impl QueueStore for InMemoryQueueStore {
    async fn enqueue(
        &self,
        sender: &str,
        recipient: &str,
        domain: &str,
        message_data: &[u8],
        message_id: Option<&str>,
        now: i64,
        is_forwarded: bool,
    ) -> Result<i64, StoreError> {
        let mut s = self.state.lock().unwrap();
        Ok(Self::insert_msg(
            &mut s,
            sender,
            recipient,
            domain,
            message_data,
            message_id,
            now,
            now,
            is_forwarded,
        ))
    }

    async fn enqueue_scheduled(
        &self,
        sender: &str,
        recipient: &str,
        domain: &str,
        message_data: &[u8],
        message_id: Option<&str>,
        created_at: i64,
        scheduled_at: i64,
    ) -> Result<i64, StoreError> {
        let mut s = self.state.lock().unwrap();
        Ok(Self::insert_msg(
            &mut s,
            sender,
            recipient,
            domain,
            message_data,
            message_id,
            scheduled_at,
            created_at,
            false,
        ))
    }

    async fn dequeue(&self, now: i64, limit: u32) -> Result<Vec<QueuedMessage>, StoreError> {
        let s = self.state.lock().unwrap();
        let mut out: Vec<QueuedMessage> = s
            .messages
            .iter()
            .filter(|m| m.status == QueueStatus::Pending && m.next_retry <= now)
            .cloned()
            .collect();
        out.sort_by_key(|m| m.next_retry);
        out.truncate(limit as usize);
        Ok(out)
    }

    async fn recover_stale_inflight(&self, now: i64) -> Result<u64, StoreError> {
        let threshold = now - 600;
        let mut s = self.state.lock().unwrap();
        let mut affected = 0u64;
        for m in s.messages.iter_mut() {
            if m.status == QueueStatus::InFlight && m.updated_at < threshold {
                m.status = QueueStatus::Pending;
                m.updated_at = now;
                affected += 1;
            }
        }
        Ok(affected)
    }

    async fn mark_inflight(&self, id: i64, now: i64) -> Result<(), StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(m) = s.messages.iter_mut().find(|m| m.id == id) {
            m.status = QueueStatus::InFlight;
            m.updated_at = now;
        }
        Ok(())
    }

    async fn mark_delivered(&self, id: i64, now: i64) -> Result<(), StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(m) = s.messages.iter_mut().find(|m| m.id == id) {
            m.status = QueueStatus::Delivered;
            m.updated_at = now;
        }
        Ok(())
    }

    async fn mark_failed(
        &self,
        id: i64,
        error: &str,
        next_retry: i64,
        now: i64,
    ) -> Result<(), StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(m) = s.messages.iter_mut().find(|m| m.id == id) {
            m.status = QueueStatus::Pending;
            m.attempts += 1;
            m.last_error = Some(error.into());
            m.next_retry = next_retry;
            m.updated_at = now;
        }
        Ok(())
    }

    async fn mark_bounced(&self, id: i64, error: &str, now: i64) -> Result<(), StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(m) = s.messages.iter_mut().find(|m| m.id == id) {
            m.status = QueueStatus::Bounced;
            m.last_error = Some(error.into());
            m.updated_at = now;
        }
        Ok(())
    }

    async fn get_message(&self, id: i64) -> Result<Option<QueuedMessage>, StoreError> {
        let s = self.state.lock().unwrap();
        Ok(s.messages.iter().find(|m| m.id == id).cloned())
    }

    async fn queue_stats(&self) -> Result<Vec<(String, i64)>, StoreError> {
        use std::collections::HashMap;
        let s = self.state.lock().unwrap();
        let mut counts: HashMap<&'static str, i64> = HashMap::new();
        for m in &s.messages {
            *counts.entry(m.status.as_str()).or_insert(0) += 1;
        }
        Ok(counts
            .into_iter()
            .map(|(k, v)| (k.to_string(), v))
            .collect())
    }

    async fn list_recent(&self, limit: i32) -> Result<Vec<QueuedMessage>, StoreError> {
        let s = self.state.lock().unwrap();
        let mut out: Vec<QueuedMessage> = s.messages.clone();
        out.sort_by_key(|m| std::cmp::Reverse(m.created_at));
        out.truncate(limit.max(0) as usize);
        Ok(out)
    }

    async fn cancel_pending(&self, id: i64) -> Result<bool, StoreError> {
        let mut s = self.state.lock().unwrap();
        let before = s.messages.len();
        s.messages
            .retain(|m| !(m.id == id && m.status == QueueStatus::Pending));
        Ok(s.messages.len() < before)
    }

    async fn cancel_pending_by_message_id(
        &self,
        message_id: &str,
        sender: &str,
    ) -> Result<bool, StoreError> {
        let mut s = self.state.lock().unwrap();
        let before = s.messages.len();
        s.messages.retain(|m| {
            !(m.status == QueueStatus::Pending
                && m.sender == sender
                && m.message_id.as_deref() == Some(message_id))
        });
        Ok(s.messages.len() < before)
    }

    async fn retry_message(&self, id: i64, now: i64) -> Result<bool, StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(m) = s.messages.iter_mut().find(|m| {
            m.id == id && (m.status == QueueStatus::Bounced || m.status == QueueStatus::Failed)
        }) {
            m.status = QueueStatus::Pending;
            m.next_retry = now;
            m.updated_at = now;
            return Ok(true);
        }
        Ok(false)
    }

    async fn is_suppressed(&self, email: &str) -> bool {
        let s = self.state.lock().unwrap();
        s.suppressions.iter().any(|(e, _, _, _)| e == email)
    }

    async fn add_suppression(
        &self,
        email: &str,
        reason: &str,
        smtp_code: Option<i32>,
    ) -> Result<(), StoreError> {
        let mut s = self.state.lock().unwrap();
        if let Some(entry) = s.suppressions.iter_mut().find(|(e, _, _, _)| e == email) {
            entry.1 = reason.into();
            entry.2 = smtp_code;
        } else {
            let now = chrono::Utc::now().timestamp();
            s.suppressions
                .push((email.into(), reason.into(), smtp_code, now));
        }
        Ok(())
    }

    async fn remove_suppression(&self, email: &str) -> Result<bool, StoreError> {
        let mut s = self.state.lock().unwrap();
        let before = s.suppressions.len();
        s.suppressions.retain(|(e, _, _, _)| e != email);
        Ok(s.suppressions.len() < before)
    }

    async fn list_suppressions(
        &self,
        limit: i64,
    ) -> Result<Vec<(String, String, Option<i32>, i64)>, StoreError> {
        let s = self.state.lock().unwrap();
        let mut out = s.suppressions.clone();
        out.sort_by_key(|(_, _, _, t)| std::cmp::Reverse(*t));
        out.truncate(limit.max(0) as usize);
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::time::Duration;

    fn store() -> Arc<dyn QueueStore> {
        Arc::new(InMemoryQueueStore::new())
    }

    #[tokio::test]
    async fn enqueue_dequeue_roundtrip() {
        let s = store();
        let id = s
            .enqueue("a@x", "b@y", "y", b"raw", None, 0, false)
            .await
            .unwrap();
        let msgs = s.dequeue(0, 10).await.unwrap();
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].id, id);
        assert_eq!(msgs[0].sender, "a@x");
    }

    #[tokio::test]
    async fn lifecycle_transitions() {
        let s = store();
        let id = s
            .enqueue("a@x", "b@y", "y", b"", None, 0, false)
            .await
            .unwrap();
        s.mark_inflight(id, 10).await.unwrap();
        s.mark_delivered(id, 20).await.unwrap();
        assert_eq!(
            s.get_message(id).await.unwrap().unwrap().status,
            QueueStatus::Delivered
        );
    }

    #[tokio::test]
    async fn stale_inflight_recovers() {
        let s = store();
        let id = s
            .enqueue("a@x", "b@y", "y", b"", None, 0, false)
            .await
            .unwrap();
        s.mark_inflight(id, 0).await.unwrap();
        // 10 minutes + 1 second later, recover_stale should kick it back
        let n = s.recover_stale_inflight(601).await.unwrap();
        assert_eq!(n, 1);
        assert_eq!(
            s.get_message(id).await.unwrap().unwrap().status,
            QueueStatus::Pending
        );
    }

    #[tokio::test]
    async fn suppression_list() {
        let s = store();
        assert!(!s.is_suppressed("user@x").await);
        s.add_suppression("user@x", "bounced", Some(550))
            .await
            .unwrap();
        assert!(s.is_suppressed("user@x").await);
        assert!(s.remove_suppression("user@x").await.unwrap());
        assert!(!s.is_suppressed("user@x").await);
    }

    #[tokio::test]
    async fn in_memory_notifier_wakes_waiter() {
        let n = Arc::new(InMemoryNotifier::new());
        let n2 = n.clone();
        let h = tokio::spawn(async move {
            tokio::time::timeout(Duration::from_secs(2), n2.wait())
                .await
                .expect("waiter timed out")
        });
        n.notify().await;
        h.await.unwrap();
    }
}