From 092cf4c350def60f0dd5e0800cefe99938e51b8b Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:45:19 +0100 Subject: [PATCH 1/8] feat(store): durable outbox with shared bodies and retry scheduling - Add outbox_msgs and outbox tables - Enqueue every recipient of a post in one transaction - Add due, list, complete, retry and stats queries - Delete the shared body with the last recipient --- internal/store/outbox.go | 156 ++++++++++++++++++++++++++++ internal/store/outbox_test.go | 190 ++++++++++++++++++++++++++++++++++ internal/store/store.go | 17 +++ 3 files changed, 363 insertions(+) create mode 100644 internal/store/outbox.go create mode 100644 internal/store/outbox_test.go diff --git a/internal/store/outbox.go b/internal/store/outbox.go new file mode 100644 index 0000000..37790c5 --- /dev/null +++ b/internal/store/outbox.go @@ -0,0 +1,156 @@ +package store + +import ( + "database/sql" + "errors" + "time" +) + +// NewOutbound is one recipient to queue. +type NewOutbound struct { + From string // envelope sender (VERP address) + Rcpt string + MemberID int64 // 0 for system mail + Prefix []byte // per-recipient headers, sent before the shared message +} + +// OutboxItem is a queued delivery to one recipient. +type OutboxItem struct { + ID int64 + MsgID int64 + From string + Rcpt string + MemberID int64 + Prefix []byte + Attempts int + NextAt time.Time + LastError string + CreatedAt time.Time +} + +// OutboxStats summarises the queue. +type OutboxStats struct { + Pending int // every queued delivery + Retrying int // deliveries that already failed at least once + Oldest time.Time // zero when the queue is empty +} + +// Enqueue stores a message and one delivery per recipient, all or nothing. +func (s *Store) Enqueue(msg []byte, items []NewOutbound) error { + if len(items) == 0 { + return nil + } + tx, err := s.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + res, err := tx.Exec(`INSERT INTO outbox_msgs(data) VALUES(?)`, msg) + if err != nil { + return err + } + msgID, _ := res.LastInsertId() + now := s.Now().Unix() + for _, it := range items { + prefix := it.Prefix + if prefix == nil { + prefix = []byte{} + } + if _, err := tx.Exec(`INSERT INTO outbox(msg_id, envelope_from, rcpt, member_id, prefix, next_at, created_at) + VALUES(?,?,?,?,?,?,?)`, msgID, it.From, it.Rcpt, it.MemberID, prefix, now, now); err != nil { + return err + } + } + return tx.Commit() +} + +const outboxCols = `id, msg_id, envelope_from, rcpt, member_id, prefix, attempts, next_at, last_error, created_at` + +func scanOutbox(row interface{ Scan(...any) error }) (OutboxItem, error) { + var it OutboxItem + var next, created int64 + err := row.Scan(&it.ID, &it.MsgID, &it.From, &it.Rcpt, &it.MemberID, &it.Prefix, &it.Attempts, &next, &it.LastError, &created) + it.NextAt, it.CreatedAt = time.Unix(next, 0), time.Unix(created, 0) + return it, err +} + +func (s *Store) queryOutbox(q string, args ...any) ([]OutboxItem, error) { + rows, err := s.db.Query(q, args...) + if err != nil { + return nil, err + } + defer rows.Close() + var out []OutboxItem + for rows.Next() { + it, err := scanOutbox(rows) + if err != nil { + return nil, err + } + out = append(out, it) + } + return out, rows.Err() +} + +// DueOutbox returns up to limit deliveries whose time has come, longest waiting first. +func (s *Store) DueOutbox(limit int) ([]OutboxItem, error) { + return s.queryOutbox(`SELECT `+outboxCols+` FROM outbox WHERE next_at<=? ORDER BY next_at, id LIMIT ?`, + s.Now().Unix(), limit) +} + +// ListOutbox returns up to limit deliveries for display, failing ones first. +func (s *Store) ListOutbox(limit int) ([]OutboxItem, error) { + return s.queryOutbox(`SELECT `+outboxCols+` FROM outbox ORDER BY attempts DESC, next_at, id LIMIT ?`, limit) +} + +// OutboxData returns the shared message of a queued delivery. +func (s *Store) OutboxData(msgID int64) ([]byte, error) { + var b []byte + err := s.db.QueryRow(`SELECT data FROM outbox_msgs WHERE id=?`, msgID).Scan(&b) + if errors.Is(err, sql.ErrNoRows) { + return nil, ErrNotFound + } + return b, err +} + +// CompleteOutbox removes a delivery (sent, dropped or discarded) and the shared +// message once no recipient needs it. +func (s *Store) CompleteOutbox(id int64) error { + tx, err := s.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + var msgID int64 + err = tx.QueryRow(`SELECT msg_id FROM outbox WHERE id=?`, id).Scan(&msgID) + if errors.Is(err, sql.ErrNoRows) { + return ErrNotFound + } + if err != nil { + return err + } + if _, err := tx.Exec(`DELETE FROM outbox WHERE id=?`, id); err != nil { + return err + } + if _, err := tx.Exec(`DELETE FROM outbox_msgs WHERE id=? AND NOT EXISTS (SELECT 1 FROM outbox WHERE msg_id=?)`, msgID, msgID); err != nil { + return err + } + return tx.Commit() +} + +// RetryOutbox records a failed attempt and schedules the next one. +func (s *Store) RetryOutbox(id int64, next time.Time, lastError string) error { + return affected(s.db.Exec(`UPDATE outbox SET attempts=attempts+1, next_at=?, last_error=? WHERE id=?`, + next.Unix(), lastError, id)) +} + +// OutboxStats returns queue totals. +func (s *Store) OutboxStats() (OutboxStats, error) { + var st OutboxStats + var oldest sql.NullInt64 + err := s.db.QueryRow(`SELECT COUNT(*), COALESCE(SUM(attempts>0),0), MIN(created_at) FROM outbox`). + Scan(&st.Pending, &st.Retrying, &oldest) + if oldest.Valid { + st.Oldest = time.Unix(oldest.Int64, 0) + } + return st, err +} diff --git a/internal/store/outbox_test.go b/internal/store/outbox_test.go new file mode 100644 index 0000000..9e88886 --- /dev/null +++ b/internal/store/outbox_test.go @@ -0,0 +1,190 @@ +package store + +import ( + "errors" + "testing" + "time" +) + +func items(n int) []NewOutbound { + out := make([]NewOutbound, n) + for i := range out { + out[i] = NewOutbound{From: "bounce@x.org", Rcpt: string(rune('a'+i)) + "@x.com", MemberID: int64(i + 1), Prefix: []byte("List-Unsubscribe: \r\n")} + } + return out +} + +func TestEnqueueAndDue(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_000_000, 0) + s.Now = func() time.Time { return now } + if err := s.Enqueue([]byte("Subject: x\r\n\r\nbody"), items(3)); err != nil { + t.Fatal(err) + } + due, err := s.DueOutbox(10) + if err != nil || len(due) != 3 { + t.Fatalf("due: %d %v", len(due), err) + } + d := due[0] + if d.From != "bounce@x.org" || d.Rcpt != "a@x.com" || d.MemberID != 1 || string(d.Prefix) != "List-Unsubscribe: \r\n" || d.Attempts != 0 { + t.Fatalf("item: %+v", d) + } + data, err := s.OutboxData(d.MsgID) + if err != nil || string(data) != "Subject: x\r\n\r\nbody" { + t.Fatalf("data: %q %v", data, err) + } + if due[0].MsgID != due[2].MsgID { + t.Fatal("recipients of one post must share the stored body") + } + if got, _ := s.DueOutbox(2); len(got) != 2 { + t.Fatalf("limit not applied: %d", len(got)) + } +} + +func TestEnqueueNothingStoresNothing(t *testing.T) { + s := newTestStore(t) + if err := s.Enqueue([]byte("x"), nil); err != nil { + t.Fatal(err) + } + var n int + s.db.QueryRow(`SELECT COUNT(*) FROM outbox_msgs`).Scan(&n) + if n != 0 { + t.Fatalf("orphan message stored: %d", n) + } +} + +func TestCompleteDeletesTheBodyWithTheLastRecipient(t *testing.T) { + s := newTestStore(t) + s.Enqueue([]byte("body"), items(2)) + due, _ := s.DueOutbox(10) + if err := s.CompleteOutbox(due[0].ID); err != nil { + t.Fatal(err) + } + if _, err := s.OutboxData(due[0].MsgID); err != nil { + t.Fatalf("body must stay while another recipient waits: %v", err) + } + if err := s.CompleteOutbox(due[1].ID); err != nil { + t.Fatal(err) + } + if _, err := s.OutboxData(due[0].MsgID); !errors.Is(err, ErrNotFound) { + t.Fatalf("body must go with the last recipient: %v", err) + } + if err := s.CompleteOutbox(due[1].ID); !errors.Is(err, ErrNotFound) { + t.Fatalf("want ErrNotFound, got %v", err) + } +} + +func TestRetryDelaysAndCounts(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_000_000, 0) + s.Now = func() time.Time { return now } + s.Enqueue([]byte("body"), items(2)) + due, _ := s.DueOutbox(10) + next := now.Add(5 * time.Minute) + if err := s.RetryOutbox(due[0].ID, next, "connection refused"); err != nil { + t.Fatal(err) + } + got, _ := s.DueOutbox(10) + if len(got) != 1 || got[0].ID != due[1].ID { + t.Fatalf("the retried item must wait: %+v", got) + } + now = next.Add(-time.Second) + if got, _ := s.DueOutbox(10); len(got) != 1 { + t.Fatal("not due one second early") + } + now = next + got, _ = s.DueOutbox(10) + if len(got) != 2 { + t.Fatalf("due again at next_at: %d", len(got)) + } + var retried OutboxItem + for _, g := range got { + if g.ID == due[0].ID { + retried = g + } + } + if retried.Attempts != 1 || retried.LastError != "connection refused" || !retried.NextAt.Equal(next) { + t.Fatalf("retried: %+v", retried) + } + if err := s.RetryOutbox(999, next, "x"); !errors.Is(err, ErrNotFound) { + t.Fatalf("want ErrNotFound, got %v", err) + } +} + +func TestDueIsOrderedByNextAttempt(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_000_000, 0) + s.Now = func() time.Time { return now } + s.Enqueue([]byte("body"), items(3)) + due, _ := s.DueOutbox(10) + s.RetryOutbox(due[0].ID, now.Add(-time.Hour), "e") // oldest next_at + now = now.Add(time.Minute) + got, _ := s.DueOutbox(10) + if got[0].ID != due[0].ID { + t.Fatalf("the longest waiting item goes first: %+v", got) + } +} + +func TestOutboxStats(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_000_000, 0) + s.Now = func() time.Time { return now } + st, err := s.OutboxStats() + if err != nil || st.Pending != 0 || st.Retrying != 0 || !st.Oldest.IsZero() { + t.Fatalf("empty: %+v %v", st, err) + } + s.Enqueue([]byte("body"), items(3)) + due, _ := s.DueOutbox(10) + s.RetryOutbox(due[0].ID, now.Add(time.Minute), "boom") + now = now.Add(10 * time.Second) + st, _ = s.OutboxStats() + if st.Pending != 3 || st.Retrying != 1 || !st.Oldest.Equal(time.Unix(1_000_000, 0)) { + t.Fatalf("stats: %+v", st) + } +} + +func TestListOutbox(t *testing.T) { + s := newTestStore(t) + s.Enqueue([]byte("body"), items(5)) + due, _ := s.DueOutbox(10) + s.RetryOutbox(due[3].ID, time.Now().Add(time.Hour), "refused") + got, err := s.ListOutbox(3) + if err != nil || len(got) != 3 { + t.Fatalf("list: %d %v", len(got), err) + } + failing, _ := s.ListOutbox(10) + if failing[0].ID != due[3].ID { + t.Fatalf("items that failed come first: %+v", failing[0]) + } +} + +func TestDiscardOutbox(t *testing.T) { + s := newTestStore(t) + s.Enqueue([]byte("body"), items(1)) + due, _ := s.DueOutbox(10) + if err := s.CompleteOutbox(due[0].ID); err != nil { + t.Fatal(err) + } + if err := s.CompleteOutbox(due[0].ID); !errors.Is(err, ErrNotFound) { + t.Fatalf("want ErrNotFound, got %v", err) + } +} + +func TestQueueSurvivesReopen(t *testing.T) { + path := t.TempDir() + "/q.db" + s, _ := Open(path) + s.Enqueue([]byte("persist me"), items(2)) + s.Close() + s2, err := Open(path) + if err != nil { + t.Fatal(err) + } + defer s2.Close() + due, _ := s2.DueOutbox(10) + if len(due) != 2 { + t.Fatalf("queue lost on restart: %d", len(due)) + } + if data, _ := s2.OutboxData(due[0].MsgID); string(data) != "persist me" { + t.Fatalf("data: %q", data) + } +} diff --git a/internal/store/store.go b/internal/store/store.go index 4572fc4..e7fee0f 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -152,6 +152,23 @@ END; CREATE TRIGGER IF NOT EXISTS archive_ad AFTER DELETE ON archive BEGIN INSERT INTO archive_fts(archive_fts, rowid, subject, body, sender) VALUES ('delete', old.id, old.subject, old.body, old.sender); END; +CREATE TABLE IF NOT EXISTS outbox_msgs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + data BLOB NOT NULL +); +CREATE TABLE IF NOT EXISTS outbox ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + msg_id INTEGER NOT NULL REFERENCES outbox_msgs(id) ON DELETE CASCADE, + envelope_from TEXT NOT NULL, + rcpt TEXT NOT NULL, + member_id INTEGER NOT NULL DEFAULT 0, + prefix BLOB NOT NULL DEFAULT x'', + attempts INTEGER NOT NULL DEFAULT 0, + next_at INTEGER NOT NULL, + last_error TEXT NOT NULL DEFAULT '', + created_at INTEGER NOT NULL +); +CREATE INDEX IF NOT EXISTS outbox_due ON outbox(next_at, id); CREATE TABLE IF NOT EXISTS settings ( key TEXT PRIMARY KEY, value TEXT NOT NULL From ecbab4d18c7e645a4c86c2efb8d23825a3a1a4f5 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:46:27 +0100 Subject: [PATCH 2/8] feat(outbound): classify relay errors as permanent or bad mailbox - Add IsPermanent for SMTP 5xx and PermanentError - Add IsBadMailbox for enhanced status 5.1.x only, so a misconfigured relay says nothing about recipients --- internal/outbound/outbound.go | 34 +++++++++++++++ internal/outbound/outbound_test.go | 68 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/internal/outbound/outbound.go b/internal/outbound/outbound.go index 9ca977a..cd374ab 100644 --- a/internal/outbound/outbound.go +++ b/internal/outbound/outbound.go @@ -4,6 +4,7 @@ package outbound import ( "bytes" "context" + "errors" "fmt" "net" "sync" @@ -79,3 +80,36 @@ func (f *Fake) Send(_ context.Context, from string, to []string, data []byte) er f.Sent = append(f.Sent, Sent{from, append([]string(nil), to...), append([]byte(nil), data...)}) return nil } + +// PermanentError marks a failure that retrying cannot fix. SMTP 5xx replies are +// permanent without being wrapped in it; use it for other sources (and in tests). +type PermanentError struct { + Err error + BadMailbox bool // the destination mailbox is invalid +} + +func (e *PermanentError) Error() string { return e.Err.Error() } +func (e *PermanentError) Unwrap() error { return e.Err } + +// IsPermanent reports whether err is a permanent failure: an SMTP 5xx reply or a PermanentError. +func IsPermanent(err error) bool { + var pe *PermanentError + if errors.As(err, &pe) { + return true + } + var se *smtp.SMTPError + return errors.As(err, &se) && se.Code >= 500 && se.Code <= 599 +} + +// IsBadMailbox reports whether err says the destination mailbox is invalid (SMTP +// enhanced status 5.1.x). Other permanent errors, such as "relay access denied", +// say nothing about the recipient. +func IsBadMailbox(err error) bool { + var pe *PermanentError + if errors.As(err, &pe) { + return pe.BadMailbox + } + var se *smtp.SMTPError + return errors.As(err, &se) && se.Code >= 500 && se.Code <= 599 && + se.EnhancedCode[0] == 5 && se.EnhancedCode[1] == 1 +} diff --git a/internal/outbound/outbound_test.go b/internal/outbound/outbound_test.go index 0e68675..0459e55 100644 --- a/internal/outbound/outbound_test.go +++ b/internal/outbound/outbound_test.go @@ -2,6 +2,7 @@ package outbound import ( "context" + "errors" "fmt" "io" "net" @@ -128,3 +129,70 @@ func TestSMTPRelayDefaultBatchSize(t *testing.T) { t.Fatalf("default batch size must be %d, got %d transactions", DefaultBatchSize, len(cap.msgs)) } } + +func TestClassifyErrors(t *testing.T) { + smtpErr := func(code int, enh smtp.EnhancedCode) error { + return &smtp.SMTPError{Code: code, EnhancedCode: enh, Message: "x"} + } + cases := []struct { + name string + err error + perm, mailbox bool + }{ + {"nil", nil, false, false}, + {"plain", errors.New("connection refused"), false, false}, + {"451", smtpErr(451, smtp.EnhancedCode{4, 3, 0}), false, false}, + {"421", smtpErr(421, smtp.NoEnhancedCode), false, false}, + {"550 unknown mailbox", smtpErr(550, smtp.EnhancedCode{5, 1, 1}), true, true}, + {"553 bad address syntax", smtpErr(553, smtp.EnhancedCode{5, 1, 3}), true, true}, + {"554 relay denied", smtpErr(554, smtp.EnhancedCode{5, 7, 1}), true, false}, + {"550 no enhanced code", smtpErr(550, smtp.NoEnhancedCode), true, false}, + {"wrapped", fmt.Errorf("batch 0-1: %w", smtpErr(550, smtp.EnhancedCode{5, 1, 1})), true, true}, + {"explicit permanent", &PermanentError{Err: errors.New("no")}, true, false}, + {"explicit bad mailbox", &PermanentError{Err: errors.New("no"), BadMailbox: true}, true, true}, + } + for _, c := range cases { + if got := IsPermanent(c.err); got != c.perm { + t.Errorf("%s: IsPermanent = %v", c.name, got) + } + if got := IsBadMailbox(c.err); got != c.mailbox { + t.Errorf("%s: IsBadMailbox = %v", c.name, got) + } + } +} + +type rejecting struct{ err error } + +func (r rejecting) NewSession(*smtp.Conn) (smtp.Session, error) { return &rejectSession{r.err}, nil } + +type rejectSession struct{ err error } + +func (s *rejectSession) Reset() {} +func (s *rejectSession) Logout() error { return nil } +func (s *rejectSession) Mail(string, *smtp.MailOptions) error { return nil } +func (s *rejectSession) Rcpt(string, *smtp.RcptOptions) error { return s.err } +func (s *rejectSession) Data(io.Reader) error { return nil } + +func TestRelayRejectionsAreClassifiedFromTheWire(t *testing.T) { + for name, tc := range map[string]struct { + err error + perm, mailbox bool + }{ + "unknown user": {&smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user"}, true, true}, + "relay denied": {&smtp.SMTPError{Code: 554, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "relay access denied"}, true, false}, + "try later": {&smtp.SMTPError{Code: 451, EnhancedCode: smtp.EnhancedCode{4, 3, 0}, Message: "try later"}, false, false}, + } { + srv := smtp.NewServer(rejecting{tc.err}) + ln, _ := net.Listen("tcp", "127.0.0.1:0") + go srv.Serve(ln) + r := &SMTPRelay{Addr: ln.Addr().String()} + err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n")) + srv.Close() + if err == nil { + t.Fatalf("%s: want an error", name) + } + if IsPermanent(err) != tc.perm || IsBadMailbox(err) != tc.mailbox { + t.Errorf("%s: permanent=%v mailbox=%v for %v", name, IsPermanent(err), IsBadMailbox(err), err) + } + } +} From e6713962f93f2f4e7b7680175accd64ede419cda Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:47:14 +0100 Subject: [PATCH 3/8] feat(queue): background worker with retries, backoff and bounded concurrency - Add Worker.RunOnce, Run and Wake over the durable outbox - Send prefix plus shared message per recipient with a per-send timeout - Retry temporary failures after 1m, 5m, 15m, 1h, 3h, then every 6h; give up after 3 days - Drop permanent failures; count a hard bounce only for bad-mailbox replies - Leave interrupted sends queued on shutdown - Share the bounce threshold between router and worker --- internal/queue/queue.go | 211 +++++++++++++++++ internal/queue/queue_test.go | 423 +++++++++++++++++++++++++++++++++++ internal/router/commands.go | 5 +- internal/store/store.go | 3 + 4 files changed, 638 insertions(+), 4 deletions(-) create mode 100644 internal/queue/queue.go create mode 100644 internal/queue/queue_test.go diff --git a/internal/queue/queue.go b/internal/queue/queue.go new file mode 100644 index 0000000..acb8ae1 --- /dev/null +++ b/internal/queue/queue.go @@ -0,0 +1,211 @@ +// Package queue delivers the outbox to the SMTP relay in the background. +package queue + +import ( + "context" + "errors" + "log" + "sync" + "time" + + "codeberg.org/squidd/parsimony/internal/outbound" + "codeberg.org/squidd/parsimony/internal/store" +) + +// MaxAge is how long a failing delivery is retried before it is dropped. +const MaxAge = 72 * time.Hour + +// Defaults for the zero values of Worker fields. +const ( + defaultConcurrency = 4 + defaultBatch = 50 + defaultTimeout = time.Minute + defaultInterval = 5 * time.Second +) + +var backoff = []time.Duration{time.Minute, 5 * time.Minute, 15 * time.Minute, time.Hour, 3 * time.Hour, 6 * time.Hour} + +// Backoff returns how long to wait after the n-th consecutive failure (n >= 1). +func Backoff(failures int) time.Duration { + if failures < 1 { + failures = 1 + } + if failures > len(backoff) { + failures = len(backoff) + } + return backoff[failures-1] +} + +// Stats counts what one run did. +type Stats struct { + Sent int // delivered to the relay + Retried int // failed temporarily, scheduled again + Dropped int // removed: permanent failure or too old + Bounced int // dropped deliveries that counted as a hard bounce +} + +// Worker sends queued deliveries. Zero values of the optional fields mean defaults. +type Worker struct { + Store *store.Store + Sender outbound.Sender + Concurrency int // simultaneous sends + Batch int // deliveries taken per run + Timeout time.Duration // per send + Interval time.Duration // idle polling, to pick up retries that have become due + + wakeOnce sync.Once + wake chan struct{} + runMu sync.Mutex +} + +func (w *Worker) wakeCh() chan struct{} { + w.wakeOnce.Do(func() { w.wake = make(chan struct{}, 1) }) + return w.wake +} + +// Wake asks Run to look at the queue now. It never blocks. +func (w *Worker) Wake() { + select { + case w.wakeCh() <- struct{}{}: + default: + } +} + +// Run delivers until ctx is cancelled. +func (w *Worker) Run(ctx context.Context) { + interval := w.Interval + if interval <= 0 { + interval = defaultInterval + } + timer := time.NewTimer(0) + defer timer.Stop() + for { + select { + case <-ctx.Done(): + return + case <-w.wakeCh(): + case <-timer.C: + } + // Keep going while there is work, so a big post drains without waiting for the timer. + for ctx.Err() == nil { + st, err := w.RunOnce(ctx) + if err != nil { + log.Printf("queue: %v", err) + break + } + if st == (Stats{}) { + break + } + } + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + timer.Reset(interval) + } +} + +// RunOnce attempts every delivery that is due, up to Batch, with bounded concurrency. +func (w *Worker) RunOnce(ctx context.Context) (Stats, error) { + w.runMu.Lock() + defer w.runMu.Unlock() + + batch := w.Batch + if batch <= 0 { + batch = defaultBatch + } + conc := w.Concurrency + if conc <= 0 { + conc = defaultConcurrency + } + due, err := w.Store.DueOutbox(batch) + if err != nil { + return Stats{}, err + } + + var ( + mu sync.Mutex + stats Stats + wg sync.WaitGroup + sem = make(chan struct{}, conc) + ) + for _, it := range due { + if ctx.Err() != nil { + break + } + sem <- struct{}{} + wg.Add(1) + go func(it store.OutboxItem) { + defer wg.Done() + defer func() { <-sem }() + o := w.deliver(ctx, it) + mu.Lock() + stats.Sent += o.Sent + stats.Retried += o.Retried + stats.Dropped += o.Dropped + stats.Bounced += o.Bounced + mu.Unlock() + }(it) + } + wg.Wait() + return stats, nil +} + +func (w *Worker) deliver(ctx context.Context, it store.OutboxItem) Stats { + data, err := w.Store.OutboxData(it.MsgID) + if errors.Is(err, store.ErrNotFound) { + w.complete(it) + return Stats{Dropped: 1} + } + if err != nil { + log.Printf("queue: read message %d: %v", it.MsgID, err) + return Stats{} + } + msg := make([]byte, 0, len(it.Prefix)+len(data)) + msg = append(append(msg, it.Prefix...), data...) + + timeout := w.Timeout + if timeout <= 0 { + timeout = defaultTimeout + } + sctx, cancel := context.WithTimeout(ctx, timeout) + err = w.Sender.Send(sctx, it.From, []string{it.Rcpt}, msg) + cancel() + + switch { + case err == nil: + w.complete(it) + return Stats{Sent: 1} + case ctx.Err() != nil: + return Stats{} // shutting down: the delivery stays queued, unchanged + case outbound.IsPermanent(err): + out := Stats{Dropped: 1} + if outbound.IsBadMailbox(err) && it.MemberID != 0 { + if _, e := w.Store.RecordHardBounce(it.MemberID, store.BounceThreshold); e == nil { + out.Bounced = 1 + } + } + log.Printf("queue: drop %s: %v", it.Rcpt, err) + w.complete(it) + return out + } + now := w.Store.Now() + if now.Sub(it.CreatedAt) > MaxAge { + log.Printf("queue: give up on %s after %s: %v", it.Rcpt, MaxAge, err) + w.complete(it) + return Stats{Dropped: 1} + } + if e := w.Store.RetryOutbox(it.ID, now.Add(Backoff(it.Attempts+1)), err.Error()); e != nil { + log.Printf("queue: reschedule %d: %v", it.ID, e) + return Stats{} + } + return Stats{Retried: 1} +} + +func (w *Worker) complete(it store.OutboxItem) { + if err := w.Store.CompleteOutbox(it.ID); err != nil && !errors.Is(err, store.ErrNotFound) { + log.Printf("queue: remove %d: %v", it.ID, err) + } +} diff --git a/internal/queue/queue_test.go b/internal/queue/queue_test.go new file mode 100644 index 0000000..b8e185c --- /dev/null +++ b/internal/queue/queue_test.go @@ -0,0 +1,423 @@ +package queue + +import ( + "context" + "errors" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/emersion/go-smtp" + + "codeberg.org/squidd/parsimony/internal/outbound" + "codeberg.org/squidd/parsimony/internal/store" +) + +var t0 = time.Unix(1_900_000_000, 0) + +// script is a Sender whose behaviour the test controls. +type script struct { + mu sync.Mutex + sent []outbound.Sent + fn func(ctx context.Context, from string, to []string) error // nil: succeed + calls int32 +} + +func (s *script) Send(ctx context.Context, from string, to []string, data []byte) error { + atomic.AddInt32(&s.calls, 1) + if s.fn != nil { + if err := s.fn(ctx, from, to); err != nil { + return err + } + } + s.mu.Lock() + defer s.mu.Unlock() + s.sent = append(s.sent, outbound.Sent{From: from, To: append([]string(nil), to...), Data: append([]byte(nil), data...)}) + return nil +} + +func (s *script) count() int { + s.mu.Lock() + defer s.mu.Unlock() + return len(s.sent) +} + +// clock is a test clock that the worker goroutine may read while the test moves it. +type clock struct { + mu sync.Mutex + t time.Time +} + +func (c *clock) now() time.Time { + c.mu.Lock() + defer c.mu.Unlock() + return c.t +} + +func (c *clock) set(t time.Time) { + c.mu.Lock() + c.t = t + c.mu.Unlock() +} + +type env struct { + s *store.Store + w *Worker + snd *script + clk *clock +} + +func setup(t *testing.T) *env { + t.Helper() + s, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { s.Close() }) + clk := &clock{t: t0} + s.Now = clk.now + snd := &script{} + return &env{s: s, snd: snd, clk: clk, w: &Worker{Store: s, Sender: snd}} +} + +func (e *env) enqueue(t *testing.T, n int) { + t.Helper() + var items []store.NewOutbound + for i := 0; i < n; i++ { + items = append(items, store.NewOutbound{From: "bounce@x.org", Rcpt: string(rune('a'+i)) + "@x.com", Prefix: []byte("X-Rcpt: " + string(rune('a'+i)) + "\r\n")}) + } + if err := e.s.Enqueue([]byte("Subject: hi\r\n\r\nbody\r\n"), items); err != nil { + t.Fatal(err) + } +} + +func (e *env) pending(t *testing.T) int { + t.Helper() + st, _ := e.s.OutboxStats() + return st.Pending +} + +func TestDeliversPrefixPlusMessage(t *testing.T) { + e := setup(t) + e.enqueue(t, 2) + st, err := e.w.RunOnce(context.Background()) + if err != nil || st.Sent != 2 { + t.Fatalf("run: %+v %v", st, err) + } + if e.pending(t) != 0 { + t.Fatal("sent items must leave the queue") + } + got := e.snd.sent[0] + if got.From != "bounce@x.org" || len(got.To) != 1 || got.To[0] != "a@x.com" { + t.Fatalf("envelope: %+v", got) + } + if string(got.Data) != "X-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" { + t.Fatalf("data = %q", got.Data) + } + if string(e.snd.sent[1].Data) != "X-Rcpt: b\r\nSubject: hi\r\n\r\nbody\r\n" { + t.Fatalf("each recipient has its own prefix: %q", e.snd.sent[1].Data) + } +} + +func TestEmptyQueueIsFine(t *testing.T) { + e := setup(t) + if st, err := e.w.RunOnce(context.Background()); err != nil || st != (Stats{}) { + t.Fatalf("%+v %v", st, err) + } +} + +func TestBackoffSchedule(t *testing.T) { + want := []time.Duration{time.Minute, 5 * time.Minute, 15 * time.Minute, time.Hour, 3 * time.Hour, 6 * time.Hour, 6 * time.Hour, 6 * time.Hour} + for i, w := range want { + if got := Backoff(i + 1); got != w { + t.Errorf("Backoff(%d) = %v, want %v", i+1, got, w) + } + } + if Backoff(0) != time.Minute || Backoff(-3) != time.Minute { + t.Error("nonsense input falls back to the first delay") + } +} + +func TestTemporaryFailureIsRetriedLater(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + e.snd.fn = func(context.Context, string, []string) error { return errors.New("connection refused") } + st, _ := e.w.RunOnce(context.Background()) + if st.Retried != 1 || st.Sent != 0 { + t.Fatalf("stats: %+v", st) + } + list, _ := e.s.ListOutbox(10) + if len(list) != 1 || list[0].Attempts != 1 || !strings.Contains(list[0].LastError, "connection refused") || + !list[0].NextAt.Equal(t0.Add(time.Minute)) { + t.Fatalf("item: %+v", list) + } + + // Not due yet: nothing is attempted. + before := atomic.LoadInt32(&e.snd.calls) + e.clk.set(t0.Add(59 * time.Second)) + e.w.RunOnce(context.Background()) + if atomic.LoadInt32(&e.snd.calls) != before { + t.Fatal("must wait for the backoff") + } + + // Second failure waits longer. + e.clk.set(t0.Add(time.Minute)) + e.w.RunOnce(context.Background()) + list, _ = e.s.ListOutbox(10) + if list[0].Attempts != 2 || !list[0].NextAt.Equal(t0.Add(time.Minute+5*time.Minute)) { + t.Fatalf("second failure: %+v", list[0]) + } + + // The relay comes back. + e.snd.fn = nil + e.clk.set(t0.Add(10 * time.Minute)) + st, _ = e.w.RunOnce(context.Background()) + if st.Sent != 1 || e.pending(t) != 0 || e.snd.count() != 1 { + t.Fatalf("recovery: %+v pending=%d sent=%d", st, e.pending(t), e.snd.count()) + } +} + +func TestOneFailureDoesNotBlockTheOthers(t *testing.T) { + e := setup(t) + e.enqueue(t, 3) + e.snd.fn = func(_ context.Context, _ string, to []string) error { + if to[0] == "b@x.com" { + return errors.New("greylisted") + } + return nil + } + st, _ := e.w.RunOnce(context.Background()) + if st.Sent != 2 || st.Retried != 1 || e.pending(t) != 1 { + t.Fatalf("%+v pending=%d", st, e.pending(t)) + } + list, _ := e.s.ListOutbox(10) + if list[0].Rcpt != "b@x.com" { + t.Fatalf("the failed recipient stays queued, got %q", list[0].Rcpt) + } +} + +func TestPermanentFailureDropsWithoutBouncing(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + e.snd.fn = func(context.Context, string, []string) error { + return &smtp.SMTPError{Code: 554, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "relay access denied"} + } + st, _ := e.w.RunOnce(context.Background()) + if st.Dropped != 1 || st.Bounced != 0 || e.pending(t) != 0 { + t.Fatalf("%+v pending=%d", st, e.pending(t)) + } +} + +func TestBadMailboxCountsAHardBounceAndDisablesAfterThree(t *testing.T) { + e := setup(t) + l, _ := e.s.CreateList("dev", "example.org", "Dev") + e.s.AddMember(l.ID, "gone@x.com", "") + ms, _ := e.s.Members(l.ID) + id := ms[0].ID + e.snd.fn = func(context.Context, string, []string) error { + return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user"} + } + for i := 1; i <= 3; i++ { + e.s.Enqueue([]byte("Subject: x\r\n\r\nx"), []store.NewOutbound{{From: "b@x.org", Rcpt: "gone@x.com", MemberID: id}}) + st, _ := e.w.RunOnce(context.Background()) + if st.Dropped != 1 || st.Bounced != 1 { + t.Fatalf("round %d: %+v", i, st) + } + m, _ := e.s.MemberByID(id) + if m.BounceCount != i || m.Disabled != (i == 3) { + t.Fatalf("round %d: member %+v", i, m) + } + } + if e.pending(t) != 0 { + t.Fatal("permanent failures leave the queue") + } +} + +func TestBadMailboxForSystemMailOrUnknownMemberIsHarmless(t *testing.T) { + e := setup(t) + e.snd.fn = func(context.Context, string, []string) error { + return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such user"} + } + e.s.Enqueue([]byte("x"), []store.NewOutbound{ + {From: "b@x.org", Rcpt: "a@x.com", MemberID: 0}, // confirmation mail + {From: "b@x.org", Rcpt: "b@x.com", MemberID: 404}, // member was deleted meanwhile + }) + st, err := e.w.RunOnce(context.Background()) + if err != nil || st.Dropped != 2 || e.pending(t) != 0 { + t.Fatalf("%+v %v pending=%d", st, err, e.pending(t)) + } +} + +func TestGivesUpAfterMaxAge(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + e.snd.fn = func(context.Context, string, []string) error { return errors.New("still down") } + e.clk.set(t0.Add(MaxAge - time.Minute)) + if st, _ := e.w.RunOnce(context.Background()); st.Retried != 1 { + t.Fatalf("just inside the limit is retried: %+v", st) + } + e.clk.set(t0.Add(MaxAge + time.Minute)) + // The retry delay may not have passed yet; move far enough. + e.clk.set(e.clk.now().Add(7 * time.Hour)) + st, _ := e.w.RunOnce(context.Background()) + if st.Dropped != 1 || e.pending(t) != 0 { + t.Fatalf("too old: %+v pending=%d", st, e.pending(t)) + } +} + +func TestOldMailIsStillDeliveredIfTheRelayWorks(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + e.clk.set(t0.Add(MaxAge * 2)) + if st, _ := e.w.RunOnce(context.Background()); st.Sent != 1 { + t.Fatalf("age only matters for failures: %+v", st) + } +} + +func TestConcurrencyIsBounded(t *testing.T) { + e := setup(t) + e.enqueue(t, 10) + e.w.Concurrency = 3 + var cur, max int32 + e.snd.fn = func(ctx context.Context, _ string, _ []string) error { + n := atomic.AddInt32(&cur, 1) + for { + m := atomic.LoadInt32(&max) + if n <= m || atomic.CompareAndSwapInt32(&max, m, n) { + break + } + } + time.Sleep(20 * time.Millisecond) + atomic.AddInt32(&cur, -1) + return nil + } + st, _ := e.w.RunOnce(context.Background()) + if st.Sent != 10 { + t.Fatalf("%+v", st) + } + if max != 3 { + t.Fatalf("max concurrent sends = %d, want 3", max) + } +} + +func TestBatchLimitsOneRun(t *testing.T) { + e := setup(t) + e.enqueue(t, 5) + e.w.Batch = 2 + st, _ := e.w.RunOnce(context.Background()) + if st.Sent != 2 || e.pending(t) != 3 { + t.Fatalf("%+v pending=%d", st, e.pending(t)) + } +} + +func TestInterruptedSendStaysQueuedUntouched(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + ctx, cancel := context.WithCancel(context.Background()) + e.snd.fn = func(ctx context.Context, _ string, _ []string) error { + cancel() // shutdown arrives while the send is in flight + <-ctx.Done() + return ctx.Err() + } + e.w.RunOnce(ctx) + list, _ := e.s.ListOutbox(10) + if len(list) != 1 || list[0].Attempts != 0 || list[0].LastError != "" { + t.Fatalf("a shutdown is not a delivery failure: %+v", list) + } +} + +func TestSlowRelayTimesOutAndIsRetried(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + e.w.Timeout = 30 * time.Millisecond + e.snd.fn = func(ctx context.Context, _ string, _ []string) error { + <-ctx.Done() + return ctx.Err() + } + st, _ := e.w.RunOnce(context.Background()) + if st.Retried != 1 { + t.Fatalf("a hung relay is a temporary failure: %+v", st) + } + list, _ := e.s.ListOutbox(10) + if !strings.Contains(list[0].LastError, "deadline") { + t.Fatalf("last error: %q", list[0].LastError) + } +} + +func waitFor(t *testing.T, what string, cond func() bool) { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for !cond() { + if time.Now().After(deadline) { + t.Fatalf("timed out waiting for %s", what) + } + time.Sleep(5 * time.Millisecond) + } +} + +func TestRunDrainsTheQueueInBatchesAndStopsOnCancel(t *testing.T) { + e := setup(t) + e.enqueue(t, 7) + e.w.Batch = 2 + e.w.Interval = time.Hour + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { e.w.Run(ctx); close(done) }() + waitFor(t, "7 deliveries", func() bool { return e.snd.count() == 7 }) + cancel() + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("Run did not stop") + } +} + +func TestWakeTriggersAnImmediateRun(t *testing.T) { + e := setup(t) + e.w.Interval = time.Hour + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go e.w.Run(ctx) + time.Sleep(50 * time.Millisecond) // the first run finds nothing and goes idle + e.enqueue(t, 1) + e.w.Wake() + waitFor(t, "woken delivery", func() bool { return e.snd.count() == 1 }) +} + +func TestWakeBeforeRunAndRepeatedWakesDoNotBlock(t *testing.T) { + e := setup(t) + done := make(chan struct{}) + go func() { + for i := 0; i < 100; i++ { + e.w.Wake() + } + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("Wake blocked") + } +} + +func TestIdleRunPollsForDueRetries(t *testing.T) { + e := setup(t) + e.enqueue(t, 1) + fail := int32(1) + e.snd.fn = func(context.Context, string, []string) error { + if atomic.LoadInt32(&fail) == 1 { + return errors.New("down") + } + return nil + } + e.w.Interval = 20 * time.Millisecond + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go e.w.Run(ctx) + waitFor(t, "first failed attempt", func() bool { st, _ := e.s.OutboxStats(); return st.Retrying == 1 }) + atomic.StoreInt32(&fail, 0) + e.clk.set(t0.Add(2 * time.Minute)) // the backoff has passed; no Wake needed + waitFor(t, "retry by polling", func() bool { return e.snd.count() == 1 }) +} diff --git a/internal/router/commands.go b/internal/router/commands.go index 5e3b131..7b2e0f7 100644 --- a/internal/router/commands.go +++ b/internal/router/commands.go @@ -18,9 +18,6 @@ import ( "codeberg.org/squidd/parsimony/internal/store" ) -// bounceThreshold is the number of hard bounces after which a member is disabled. -const bounceThreshold = 3 - var statusRe = regexp.MustCompile(`(?mi)^Status:\s*([245])\.\d+\.\d+`) func (r *Router) secret() ([]byte, error) { @@ -170,7 +167,7 @@ func (r *Router) bounce(l store.List, tag string, data []byte) error { return err } if sm := statusRe.FindSubmatch(data); sm != nil && string(sm[1]) == "5" { - _, err = r.store.RecordHardBounce(m.ID, bounceThreshold) + _, err = r.store.RecordHardBounce(m.ID, store.BounceThreshold) } return err } diff --git a/internal/store/store.go b/internal/store/store.go index e7fee0f..ec595f1 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -41,6 +41,9 @@ const ( ActionLeave = "leave" ) +// BounceThreshold is the number of hard bounces after which a member is disabled. +const BounceThreshold = 3 + const ( pendingTTL = 72 * time.Hour pendingRateLimit = time.Hour From b207312cef4fd2df452573ba85c313a72095be8d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:47:46 +0100 Subject: [PATCH 4/8] test(queue): do not depend on the order of concurrent sends --- internal/queue/queue_test.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/queue/queue_test.go b/internal/queue/queue_test.go index b8e185c..69cc74a 100644 --- a/internal/queue/queue_test.go +++ b/internal/queue/queue_test.go @@ -109,15 +109,20 @@ func TestDeliversPrefixPlusMessage(t *testing.T) { if e.pending(t) != 0 { t.Fatal("sent items must leave the queue") } - got := e.snd.sent[0] - if got.From != "bounce@x.org" || len(got.To) != 1 || got.To[0] != "a@x.com" { - t.Fatalf("envelope: %+v", got) + // Sends run concurrently, so look recipients up instead of relying on order. + byRcpt := map[string]outbound.Sent{} + for _, m := range e.snd.sent { + byRcpt[m.To[0]] = m } - if string(got.Data) != "X-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" { - t.Fatalf("data = %q", got.Data) + a, b := byRcpt["a@x.com"], byRcpt["b@x.com"] + if a.From != "bounce@x.org" || len(a.To) != 1 { + t.Fatalf("envelope: %+v", a) } - if string(e.snd.sent[1].Data) != "X-Rcpt: b\r\nSubject: hi\r\n\r\nbody\r\n" { - t.Fatalf("each recipient has its own prefix: %q", e.snd.sent[1].Data) + if string(a.Data) != "X-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" { + t.Fatalf("data = %q", a.Data) + } + if string(b.Data) != "X-Rcpt: b\r\nSubject: hi\r\n\r\nbody\r\n" { + t.Fatalf("each recipient has its own prefix: %q", b.Data) } } From 6c95184adcc9032f6909b72bdbab056d345295ab Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:53:47 +0100 Subject: [PATCH 5/8] feat: deliver through the persistent queue - Router.Deliver queues one entry per recipient atomically; posts are accepted once queued - LMTP answers 451 only when the database fails, never because the relay is down - Confirmation mail uses the same queue - Wire the worker into the daemon: relay.concurrency config, wake on enqueue, interrupt on Close - Make SMTPRelay honour the context deadline and cancellation on a silent relay - Test restart survival, relay outage with retry, and Close with a hung relay --- internal/app/app.go | 32 +++-- internal/app/app_test.go | 143 ++++++++++++++++++++++- internal/config/config.go | 4 +- internal/config/config_test.go | 6 +- internal/ingest/ingest.go | 21 ++-- internal/ingest/ingest_test.go | 182 +++++++++++++++++++---------- internal/outbound/outbound.go | 29 ++++- internal/outbound/outbound_test.go | 66 +++++++++++ internal/router/commands.go | 20 ++-- internal/router/m2_test.go | 162 +++++++++++++++++-------- internal/router/m3_test.go | 6 +- internal/router/router.go | 42 ++++--- 12 files changed, 539 insertions(+), 174 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index ea90216..cf34233 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -19,11 +19,15 @@ import ( "codeberg.org/squidd/parsimony/internal/config" "codeberg.org/squidd/parsimony/internal/ingest" "codeberg.org/squidd/parsimony/internal/outbound" + "codeberg.org/squidd/parsimony/internal/queue" "codeberg.org/squidd/parsimony/internal/router" "codeberg.org/squidd/parsimony/internal/store" "codeberg.org/squidd/parsimony/internal/web" ) +// nowFunc is the clock of the store (queue schedules, token expiry). Tests move it. +var nowFunc = time.Now + // App is a running daemon. type App struct { // LMTPAddr and APIAddr are the addresses actually bound. @@ -38,16 +42,14 @@ type App struct { web *http.Server pub *http.Server wg sync.WaitGroup + stop context.CancelFunc // stops the queue worker once sync.Once } // releaser lets the API approve held messages through the router. -type releaser struct { - rt *router.Router - out outbound.Sender -} +type releaser struct{ rt *router.Router } -func (r releaser) Release(ctx context.Context, id int64) error { return r.rt.Release(ctx, r.out, id) } +func (r releaser) Release(_ context.Context, id int64) error { return r.rt.Release(id) } // Start opens the database and begins serving. The REST API runs only when // apiToken is not empty. @@ -56,12 +58,18 @@ func Start(cfg config.Config, apiToken string) (*App, error) { if err != nil { return nil, fmt.Errorf("store: %w", err) } + st.Now = nowFunc a := &App{store: st} rt := router.New(st) rt.BaseURL = cfg.BaseURL - relay := &outbound.SMTPRelay{Addr: cfg.Relay.Addr} - a.lmtp = ingest.NewServer(rt, relay) + worker := &queue.Worker{ + Store: st, + Sender: &outbound.SMTPRelay{Addr: cfg.Relay.Addr}, + Concurrency: cfg.Relay.Concurrency, + } + rt.OnEnqueue = worker.Wake + a.lmtp = ingest.NewServer(rt) if cfg.LMTP.Network == "unix" { os.Remove(cfg.LMTP.Addr) @@ -86,7 +94,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) { a.lns = append(a.lns, apiLn) // The admin UI and the API share a listener, so the UI needs no CORS. mux := http.NewServeMux() - mux.Handle("/api/", api.New(st, apiToken, releaser{rt, relay})) + mux.Handle("/api/", api.New(st, apiToken, releaser{rt})) mux.Handle("/", adminui.Embedded()) a.web = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second} } @@ -106,6 +114,13 @@ func Start(cfg config.Config, apiToken string) (*App, error) { a.pub = &http.Server{Handler: web.New(st, rt), ReadHeaderTimeout: 10 * time.Second} } + var workerCtx context.Context + workerCtx, a.stop = context.WithCancel(context.Background()) + a.wg.Add(1) + go func() { + defer a.wg.Done() + worker.Run(workerCtx) + }() a.wg.Add(1) go func() { defer a.wg.Done() @@ -147,6 +162,7 @@ func (a *App) Close() { if a.pub != nil { a.pub.Close() } + a.stop() // in-flight sends are interrupted and stay queued a.wg.Wait() a.store.Close() }) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index a931e22..3b47ae8 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -6,12 +6,14 @@ import ( "net/http" "strings" "sync" + "sync/atomic" "testing" "time" "github.com/emersion/go-smtp" "codeberg.org/squidd/parsimony/internal/config" + "codeberg.org/squidd/parsimony/internal/store" ) // sink is a fake SMTP relay that records what it receives. @@ -58,6 +60,18 @@ func (s *sink) count() int { return len(s.msgs) } +// waitCount waits until the relay holds n messages. +func (s *sink) waitCount(t *testing.T, n int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for s.count() < n { + if time.Now().After(deadline) { + t.Fatalf("relay has %d messages, want %d", s.count(), n) + } + time.Sleep(5 * time.Millisecond) + } +} + func (s *sink) all() []sunk { s.mu.Lock() defer s.mu.Unlock() @@ -168,16 +182,15 @@ func TestEndToEnd(t *testing.T) { if err := lmtpSend(t, a.LMTPAddr, "alice@example.com", "dev@example.org", msg); err != nil { t.Fatalf("lmtp: %v", err) } - if relay.count() != 2 { - t.Fatalf("relay got %d messages, want 2", relay.count()) - } - got := relay.msgs[0] + relay.waitCount(t, 2) + got := relay.all()[0] if !strings.Contains(got.data, "List-Id: Dev ") || !strings.HasPrefix(got.from, "dev+bounce-") { t.Fatalf("unexpected relayed mail: %+v", got) } if err := lmtpSend(t, a.LMTPAddr, "mallory@evil.com", "dev@example.org", msg); err == nil { t.Fatal("non-member must be rejected") } + time.Sleep(100 * time.Millisecond) // give a wrongly queued message time to show up if relay.count() != 2 { t.Fatal("rejected mail must not be relayed") } @@ -275,6 +288,7 @@ func TestArchiveAndUnsubscribeEndToEnd(t *testing.T) { } // The relayed copy carries a working one-click link for that recipient. + relay.waitCount(t, 2) var link string for _, m := range relay.all() { if len(m.to) == 1 && m.to[0] == "bob@example.com" { @@ -300,6 +314,8 @@ func TestArchiveAndUnsubscribeEndToEnd(t *testing.T) { "From: Alice \r\nSubject: Second\r\nMessage-ID: \r\n\r\nhi\r\n"); err != nil { t.Fatal(err) } + relay.waitCount(t, before+1) + time.Sleep(100 * time.Millisecond) if got := relay.count() - before; got != 1 { t.Fatalf("after bob left only alice should get a copy, got %d", got) } @@ -386,3 +402,122 @@ func TestAdminUIAndAPIShareTheListener(t *testing.T) { } } } + +// deadAddr returns an address nothing listens on. +func deadAddr(t *testing.T) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + ln.Close() + return addr +} + +func TestQueuedMailSurvivesARestartAndIsRetriedWhenTheRelayReturns(t *testing.T) { + var skew atomic.Int64 + nowFunc = func() time.Time { return time.Now().Add(time.Duration(skew.Load())) } + t.Cleanup(func() { nowFunc = time.Now }) + + // 1. The relay is down: the post is still accepted and stays queued. + cfg := testConfig(t, deadAddr(t)) + a, err := Start(cfg, "tok") + if err != nil { + t.Fatal(err) + } + call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, "tok") + for _, m := range []string{"alice@example.com", "bob@example.com"} { + call(t, a, "POST", "/api/v1/lists/1/members", `{"address":"`+m+`"}`, "tok") + } + msg := "From: Alice \r\nSubject: queued\r\nMessage-ID: \r\n\r\nbody\r\n" + if err := lmtpSend(t, a.LMTPAddr, "alice@example.com", "dev@example.org", msg); err != nil { + t.Fatalf("a post must be accepted while the relay is down: %v", err) + } + peek, err := store.Open(cfg.DB) + if err != nil { + t.Fatal(err) + } + defer peek.Close() + deadline := time.Now().Add(5 * time.Second) + for { + st, _ := peek.OutboxStats() + if st.Pending == 2 && st.Retrying == 2 { + break + } + if time.Now().After(deadline) { + t.Fatalf("queue after the first attempts: %+v", st) + } + time.Sleep(10 * time.Millisecond) + } + + // 2. Restart with a working relay, after the backoff has passed. + a.Close() + relayAddr, relay := startSink(t) + cfg.Relay.Addr = relayAddr + skew.Store(int64(10 * time.Minute)) + b, err := Start(cfg, "tok") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { b.Close() }) + relay.waitCount(t, 2) + got := relay.all() + if !strings.Contains(got[0].data, "List-Id: Dev ") || !strings.Contains(got[0].data, "Subject: queued") { + t.Fatalf("delivered mail: %q", got[0].data) + } + deadline = time.Now().Add(5 * time.Second) + for { + st, _ := peek.OutboxStats() + if st.Pending == 0 { + break + } + if time.Now().After(deadline) { + t.Fatalf("delivered mail must leave the queue: %+v", st) + } + time.Sleep(10 * time.Millisecond) + } +} + +func TestCloseInterruptsASlowSendWithoutLosingIt(t *testing.T) { + // A relay that accepts the connection and never answers. + ln, _ := net.Listen("tcp", "127.0.0.1:0") + t.Cleanup(func() { ln.Close() }) + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + defer c.Close() + } + }() + cfg := testConfig(t, ln.Addr().String()) + a, err := Start(cfg, "tok") + if err != nil { + t.Fatal(err) + } + call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, "tok") + call(t, a, "POST", "/api/v1/lists/1/members", `{"address":"alice@example.com"}`, "tok") + msg := "From: Alice \r\nSubject: slow\r\nMessage-ID: \r\n\r\nbody\r\n" + if err := lmtpSend(t, a.LMTPAddr, "alice@example.com", "dev@example.org", msg); err != nil { + t.Fatal(err) + } + time.Sleep(200 * time.Millisecond) // the worker is now stuck talking to the silent relay + + closed := make(chan struct{}) + go func() { a.Close(); close(closed) }() + select { + case <-closed: + case <-time.After(5 * time.Second): + t.Fatal("Close must not wait for a hung relay") + } + peek, err := store.Open(cfg.DB) + if err != nil { + t.Fatal(err) + } + defer peek.Close() + if st, _ := peek.OutboxStats(); st.Pending != 1 { + t.Fatalf("the interrupted delivery must stay queued: %+v", st) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index dd33a8b..92b60c4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,7 +24,8 @@ type Config struct { Listen string `yaml:"listen"` // public archive and unsubscribe pages; empty disables them } `yaml:"web"` Relay struct { - Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay + Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay + Concurrency int `yaml:"concurrency"` // simultaneous deliveries } `yaml:"relay"` } @@ -35,6 +36,7 @@ func Load(path string) (Config, error) { c.LMTP.Network = "tcp" c.LMTP.Addr = "127.0.0.1:2424" c.Relay.Addr = "127.0.0.1:25" + c.Relay.Concurrency = 4 c.API.Listen = "127.0.0.1:8080" c.Web.Listen = "127.0.0.1:8081" diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 0e39f69..df78e2d 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -20,17 +20,17 @@ func TestLoadDefaults(t *testing.T) { if err != nil { t.Fatal(err) } - if c.DB != "parsimony.db" || c.LMTP.Network != "tcp" || c.LMTP.Addr != "127.0.0.1:2424" || c.Relay.Addr != "127.0.0.1:25" || c.API.Listen != "127.0.0.1:8080" || c.Web.Listen != "127.0.0.1:8081" { + if c.DB != "parsimony.db" || c.LMTP.Network != "tcp" || c.LMTP.Addr != "127.0.0.1:2424" || c.Relay.Addr != "127.0.0.1:25" || c.Relay.Concurrency != 4 || c.API.Listen != "127.0.0.1:8080" || c.Web.Listen != "127.0.0.1:8081" { t.Fatalf("defaults: %+v", c) } } func TestLoadValues(t *testing.T) { - c, err := Load(write(t, "db: /var/lib/p.db\nbase_url: https://l.example.org\nlmtp:\n network: unix\n addr: /run/p.sock\nrelay:\n addr: mx:587\napi:\n listen: 0.0.0.0:9000\nweb:\n listen: 0.0.0.0:9001\n")) + c, err := Load(write(t, "db: /var/lib/p.db\nbase_url: https://l.example.org\nlmtp:\n network: unix\n addr: /run/p.sock\nrelay:\n addr: mx:587\n concurrency: 9\napi:\n listen: 0.0.0.0:9000\nweb:\n listen: 0.0.0.0:9001\n")) if err != nil { t.Fatal(err) } - if c.DB != "/var/lib/p.db" || c.BaseURL != "https://l.example.org" || c.LMTP.Network != "unix" || c.LMTP.Addr != "/run/p.sock" || c.Relay.Addr != "mx:587" || c.API.Listen != "0.0.0.0:9000" || c.Web.Listen != "0.0.0.0:9001" { + if c.DB != "/var/lib/p.db" || c.BaseURL != "https://l.example.org" || c.LMTP.Network != "unix" || c.LMTP.Addr != "/run/p.sock" || c.Relay.Addr != "mx:587" || c.Relay.Concurrency != 9 || c.API.Listen != "0.0.0.0:9000" || c.Web.Listen != "0.0.0.0:9001" { t.Fatalf("values: %+v", c) } } diff --git a/internal/ingest/ingest.go b/internal/ingest/ingest.go index 3fa60c0..1d4d8b2 100644 --- a/internal/ingest/ingest.go +++ b/internal/ingest/ingest.go @@ -2,7 +2,6 @@ package ingest import ( - "context" "errors" "io" "log" @@ -10,16 +9,16 @@ import ( "github.com/emersion/go-smtp" - "codeberg.org/squidd/parsimony/internal/outbound" "codeberg.org/squidd/parsimony/internal/router" ) const maxMessageBytes = 25 << 20 -// NewServer builds an LMTP server that routes incoming mail and relays it. +// NewServer builds an LMTP server that routes incoming mail into the outbound queue. +// A message is accepted only once it is durably queued. // Set Network/Addr on the result (or call Serve) to start it. -func NewServer(r *router.Router, out outbound.Sender) *smtp.Server { - srv := smtp.NewServer(&backend{router: r, out: out}) +func NewServer(r *router.Router) *smtp.Server { + srv := smtp.NewServer(&backend{router: r}) srv.LMTP = true srv.Domain = "localhost" srv.MaxMessageBytes = maxMessageBytes @@ -30,7 +29,6 @@ func NewServer(r *router.Router, out outbound.Sender) *smtp.Server { type backend struct { router *router.Router - out outbound.Sender } func (b *backend) NewSession(*smtp.Conn) (smtp.Session, error) { @@ -67,25 +65,22 @@ func (s *session) LMTPData(r io.Reader, status smtp.StatusCollector) error { if err != nil { return err } - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) - defer cancel() - for _, rcpt := range s.rcpts { - status.SetStatus(rcpt, s.deliver(ctx, rcpt, data)) + status.SetStatus(rcpt, s.deliver(rcpt, data)) } return nil } -func (s *session) deliver(ctx context.Context, rcpt string, data []byte) error { +func (s *session) deliver(rcpt string, data []byte) error { rt := s.b.router - if handled, err := rt.Command(ctx, s.b.out, s.from, rcpt, data); handled { + if handled, err := rt.Command(s.from, rcpt, data); handled { return s.status(rcpt, err) } res, err := rt.Route(s.from, rcpt, data) if err != nil { return s.status(rcpt, err) } - return s.status(rcpt, rt.Send(ctx, s.b.out, res)) + return s.status(rcpt, rt.Deliver(res)) } // status turns a router result into an LMTP reply. diff --git a/internal/ingest/ingest_test.go b/internal/ingest/ingest_test.go index 4535268..27d17f9 100644 --- a/internal/ingest/ingest_test.go +++ b/internal/ingest/ingest_test.go @@ -6,17 +6,26 @@ import ( "net" "strings" "testing" + "time" "github.com/emersion/go-smtp" "codeberg.org/squidd/parsimony/internal/outbound" + "codeberg.org/squidd/parsimony/internal/queue" "codeberg.org/squidd/parsimony/internal/router" "codeberg.org/squidd/parsimony/internal/store" ) const msg = "From: Alice \r\nSubject: hi\r\n\r\nbody\r\n" -func setup(t *testing.T) (addr string, relay *outbound.Fake) { +// stack is the LMTP server with a real queue worker in front of a fake relay. +type stack struct { + addr string + relay *outbound.Fake + store *store.Store +} + +func newStack(t *testing.T, sender outbound.Sender) *stack { t.Helper() s, err := store.Open(":memory:") if err != nil { @@ -27,15 +36,52 @@ func setup(t *testing.T) (addr string, relay *outbound.Fake) { s.AddMember(l.ID, "alice@example.com", "Alice") s.AddMember(l.ID, "bob@example.com", "Bob") - relay = &outbound.Fake{} - srv := NewServer(router.New(s), relay) + relay := &outbound.Fake{} + if sender == nil { + sender = relay + } + rt := router.New(s) + w := &queue.Worker{Store: s, Sender: sender, Interval: 10 * time.Millisecond} + rt.OnEnqueue = w.Wake + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { w.Run(ctx); close(done) }() + t.Cleanup(func() { cancel(); <-done }) + + srv := NewServer(rt) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } go srv.Serve(ln) t.Cleanup(func() { srv.Close() }) - return ln.Addr().String(), relay + return &stack{addr: ln.Addr().String(), relay: relay, store: s} +} + +// sent waits until the relay has received n messages and returns them. +func (st *stack) sent(t *testing.T, n int) []outbound.Sent { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for { + got := st.relay.Messages() + if len(got) >= n { + return got + } + if time.Now().After(deadline) { + t.Fatalf("relay got %d messages, want %d", len(got), n) + } + time.Sleep(5 * time.Millisecond) + } +} + +// queued is how many deliveries still wait in the outbox. +func (st *stack) queued(t *testing.T) int { + t.Helper() + stats, err := st.store.OutboxStats() + if err != nil { + t.Fatal(err) + } + return stats.Pending } // deliver sends msg over LMTP and returns the per-recipient errors. @@ -80,41 +126,45 @@ func deliver(t *testing.T, addr, from string, rcpts []string) map[string]error { } func TestDeliverFansOut(t *testing.T) { - addr, relay := setup(t) - res := deliver(t, addr, "alice@example.com", []string{"dev@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "alice@example.com", []string{"dev@example.org"}) if res["dev@example.org"] != nil { t.Fatalf("unexpected error: %v", res) } - if len(relay.Sent) != 2 { - t.Fatalf("want one send per member, got %d", len(relay.Sent)) + got := st.sent(t, 2) + if len(got) != 2 { + t.Fatalf("want one send per member, got %d", len(got)) } - s := relay.Sent[0] - if len(s.To) != 1 { - t.Errorf("recipients = %v", s.To) + m := got[0] + if len(m.To) != 1 { + t.Errorf("recipients = %v", m.To) } - if !strings.HasPrefix(s.From, "dev+bounce-") { - t.Errorf("envelope from = %q", s.From) + if !strings.HasPrefix(m.From, "dev+bounce-") { + t.Errorf("envelope from = %q", m.From) } - if !strings.Contains(string(s.Data), "List-Id: Dev ") { - t.Errorf("missing List-Id: %q", s.Data) + if !strings.Contains(string(m.Data), "List-Id: Dev ") { + t.Errorf("missing List-Id: %q", m.Data) + } + if !strings.Contains(string(m.Data), "List-Unsubscribe: ") { + t.Errorf("missing per-recipient unsubscribe header: %q", m.Data) } } func TestNonMemberRejectedPermanently(t *testing.T) { - addr, relay := setup(t) - res := deliver(t, addr, "mallory@evil.com", []string{"dev@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "mallory@evil.com", []string{"dev@example.org"}) var se *smtp.SMTPError if !errors.As(res["dev@example.org"], &se) || se.Code != 550 { t.Fatalf("want 550, got %v", res["dev@example.org"]) } - if len(relay.Sent) != 0 { - t.Fatal("nothing must be relayed") + if st.queued(t) != 0 { + t.Fatal("nothing may be queued") } } func TestUnknownListRejected(t *testing.T) { - addr, _ := setup(t) - res := deliver(t, addr, "alice@example.com", []string{"nope@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "alice@example.com", []string{"nope@example.org"}) var se *smtp.SMTPError if !errors.As(res["nope@example.org"], &se) || se.Code != 550 { t.Fatalf("want 550, got %v", res["nope@example.org"]) @@ -122,79 +172,83 @@ func TestUnknownListRejected(t *testing.T) { } func TestMixedRecipients(t *testing.T) { - addr, relay := setup(t) - res := deliver(t, addr, "alice@example.com", []string{"dev@example.org", "nope@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "alice@example.com", []string{"dev@example.org", "nope@example.org"}) if res["dev@example.org"] != nil || res["nope@example.org"] == nil { t.Fatalf("results: %v", res) } - if len(relay.Sent) != 2 { - t.Fatalf("sent = %d", len(relay.Sent)) + st.sent(t, 2) +} + +// down is a relay that refuses everything. +type down struct{} + +func (down) Send(context.Context, string, []string, []byte) error { return errors.New("relay down") } + +func TestRelayDownStillAcceptsAndKeepsTheMailQueued(t *testing.T) { + st := newStack(t, down{}) + res := deliver(t, st.addr, "alice@example.com", []string{"dev@example.org"}) + if res["dev@example.org"] != nil { + t.Fatalf("a queued post is accepted even when the relay is down: %v", res) + } + // The worker tries and schedules retries; nothing is lost. + deadline := time.Now().Add(3 * time.Second) + for { + stats, _ := st.store.OutboxStats() + if stats.Pending == 2 && stats.Retrying == 2 { + break + } + if time.Now().After(deadline) { + t.Fatalf("queue: %+v", stats) + } + time.Sleep(5 * time.Millisecond) } } -type failing struct{} - -func (failing) Send(_ context.Context, _ string, _ []string, _ []byte) error { - return errors.New("relay down") -} - -func TestRelayFailureIsTemporary(t *testing.T) { - s, _ := store.Open(":memory:") - t.Cleanup(func() { s.Close() }) - l, _ := s.CreateList("dev", "example.org", "Dev") - s.AddMember(l.ID, "alice@example.com", "Alice") - srv := NewServer(router.New(s), failing{}) - ln, _ := net.Listen("tcp", "127.0.0.1:0") - go srv.Serve(ln) - t.Cleanup(func() { srv.Close() }) - - res := deliver(t, ln.Addr().String(), "alice@example.com", []string{"dev@example.org"}) +func TestQueueFailureIsTemporary(t *testing.T) { + st := newStack(t, nil) + st.store.Close() // the database goes away under the server + res := deliver(t, st.addr, "alice@example.com", []string{"dev@example.org"}) var se *smtp.SMTPError if !errors.As(res["dev@example.org"], &se) || se.Code != 451 { - t.Fatalf("want 451, got %v", res["dev@example.org"]) + t.Fatalf("want 451 so the MTA retries, got %v", res["dev@example.org"]) } } func TestJoinCommandOverLMTP(t *testing.T) { - addr, relay := setup(t) - res := deliver(t, addr, "carol@x.com", []string{"dev+join@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "carol@x.com", []string{"dev+join@example.org"}) if res["dev+join@example.org"] != nil { t.Fatalf("join: %v", res) } - if len(relay.Sent) != 1 || relay.Sent[0].To[0] != "carol@x.com" { - t.Fatalf("confirmation mail: %+v", relay.Sent) + got := st.sent(t, 1) + if len(got[0].To) != 1 || got[0].To[0] != "carol@x.com" { + t.Fatalf("confirmation mail: %+v", got) } } func TestBadTokenRejected(t *testing.T) { - addr, _ := setup(t) - res := deliver(t, addr, "carol@x.com", []string{"dev+confirm-deadbeef@example.org"}) + st := newStack(t, nil) + res := deliver(t, st.addr, "carol@x.com", []string{"dev+confirm-deadbeef@example.org"}) var se *smtp.SMTPError if !errors.As(res["dev+confirm-deadbeef@example.org"], &se) || se.Code != 550 { t.Fatalf("want 550, got %v", res) } } -func TestModeratedPostIsAcceptedButNotRelayed(t *testing.T) { - s, _ := store.Open(":memory:") - t.Cleanup(func() { s.Close() }) - l, _ := s.CreateList("dev", "example.org", "Dev") - s.AddMember(l.ID, "alice@example.com", "Alice") - s.SetPolicies(l.ID, store.PostModerated, store.JoinOpen, store.ArchivePrivate) - relay := &outbound.Fake{} - srv := NewServer(router.New(s), relay) - ln, _ := net.Listen("tcp", "127.0.0.1:0") - go srv.Serve(ln) - t.Cleanup(func() { srv.Close() }) +func TestModeratedPostIsAcceptedButNotQueued(t *testing.T) { + st := newStack(t, nil) + l, _ := st.store.FindList("dev@example.org") + st.store.SetPolicies(l.ID, store.PostModerated, store.JoinOpen, store.ArchivePrivate) - res := deliver(t, ln.Addr().String(), "stranger@x.com", []string{"dev@example.org"}) + res := deliver(t, st.addr, "stranger@x.com", []string{"dev@example.org"}) if res["dev@example.org"] != nil { t.Fatalf("held mail must be accepted: %v", res) } - if len(relay.Sent) != 0 { - t.Fatal("nothing may be relayed") + if st.queued(t) != 0 { + t.Fatal("nothing may be queued for delivery") } - if h, _ := s.ListHeld(l.ID); len(h) != 1 { + if h, _ := st.store.ListHeld(l.ID); len(h) != 1 { t.Fatalf("held = %d", len(h)) } } diff --git a/internal/outbound/outbound.go b/internal/outbound/outbound.go index cd374ab..b53088d 100644 --- a/internal/outbound/outbound.go +++ b/internal/outbound/outbound.go @@ -43,12 +43,32 @@ func (r *SMTPRelay) Send(ctx context.Context, from string, to []string, data []b return nil } -func (r *SMTPRelay) sendBatch(ctx context.Context, from string, to []string, data []byte) error { +func (r *SMTPRelay) sendBatch(ctx context.Context, from string, to []string, data []byte) (err error) { d := net.Dialer{Timeout: 10 * time.Second} conn, err := d.DialContext(ctx, "tcp", r.Addr) if err != nil { return err } + // A relay that stops answering must not hold us past the context: apply its + // deadline to the connection and close the connection if it is cancelled. + if dl, ok := ctx.Deadline(); ok { + conn.SetDeadline(dl) + } + stop := make(chan struct{}) + defer close(stop) + go func() { + select { + case <-ctx.Done(): + conn.Close() + case <-stop: + } + }() + defer func() { + if err != nil && ctx.Err() != nil { + err = ctx.Err() // report the cause, not "use of closed connection" + } + }() + c := smtp.NewClient(conn) defer c.Close() if err := c.Hello("localhost"); err != nil { @@ -73,6 +93,13 @@ type Fake struct { Sent []Sent } +// Messages returns a copy of what was recorded; safe while sends are running. +func (f *Fake) Messages() []Sent { + f.mu.Lock() + defer f.mu.Unlock() + return append([]Sent(nil), f.Sent...) +} + // Send records the message. func (f *Fake) Send(_ context.Context, from string, to []string, data []byte) error { f.mu.Lock() diff --git a/internal/outbound/outbound_test.go b/internal/outbound/outbound_test.go index 0459e55..47b0566 100644 --- a/internal/outbound/outbound_test.go +++ b/internal/outbound/outbound_test.go @@ -8,6 +8,7 @@ import ( "net" "sync" "testing" + "time" "github.com/emersion/go-smtp" ) @@ -196,3 +197,68 @@ func TestRelayRejectionsAreClassifiedFromTheWire(t *testing.T) { } } } + +// silent accepts connections and never says a word. +func silent(t *testing.T) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + var conns []net.Conn + var mu sync.Mutex + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + mu.Lock() + conns = append(conns, c) + mu.Unlock() + } + }() + t.Cleanup(func() { + ln.Close() + mu.Lock() + for _, c := range conns { + c.Close() + } + mu.Unlock() + }) + return ln.Addr().String() +} + +func TestSendStopsWhenTheContextIsCancelled(t *testing.T) { + r := &SMTPRelay{Addr: silent(t)} + ctx, cancel := context.WithCancel(context.Background()) + errc := make(chan error, 1) + go func() { errc <- r.Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("x")) }() + time.Sleep(100 * time.Millisecond) + cancel() + select { + case err := <-errc: + if err == nil { + t.Fatal("want an error") + } + case <-time.After(2 * time.Second): + t.Fatal("Send ignored the cancelled context while talking to a silent relay") + } +} + +func TestSendHonoursTheContextDeadline(t *testing.T) { + r := &SMTPRelay{Addr: silent(t)} + ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) + defer cancel() + start := time.Now() + err := r.Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("x")) + if err == nil { + t.Fatal("want an error") + } + if took := time.Since(start); took > 2*time.Second { + t.Fatalf("took %v", took) + } + if IsPermanent(err) { + t.Fatalf("a timeout is temporary: %v", err) + } +} diff --git a/internal/router/commands.go b/internal/router/commands.go index 7b2e0f7..499c303 100644 --- a/internal/router/commands.go +++ b/internal/router/commands.go @@ -1,7 +1,6 @@ package router import ( - "context" "crypto/hmac" "crypto/sha256" "encoding/hex" @@ -14,7 +13,6 @@ import ( "strings" "time" - "codeberg.org/squidd/parsimony/internal/outbound" "codeberg.org/squidd/parsimony/internal/store" ) @@ -48,7 +46,7 @@ func (r *Router) bounceAddress(l store.List, memberID int64) string { // Command handles list command addresses such as dev+join@example.org. // handled is false when rcpt is a plain list address (use Route for those). -func (r *Router) Command(ctx context.Context, out outbound.Sender, sender, rcpt string, data []byte) (handled bool, err error) { +func (r *Router) Command(sender, rcpt string, data []byte) (handled bool, err error) { local, domain, ok := strings.Cut(strings.ToLower(strings.TrimSpace(rcpt)), "@") if !ok { return false, nil @@ -67,9 +65,9 @@ func (r *Router) Command(ctx context.Context, out outbound.Sender, sender, rcpt switch { case tag == "join": - return true, r.join(ctx, out, l, sender) + return true, r.join(l, sender) case tag == "leave": - return true, r.leave(ctx, out, l, sender) + return true, r.leave(l, sender) case strings.HasPrefix(tag, "confirm-"): return true, r.confirm(l, strings.TrimPrefix(tag, "confirm-")) case tag == "bounce": @@ -80,7 +78,7 @@ func (r *Router) Command(ctx context.Context, out outbound.Sender, sender, rcpt return true, ErrUnknownList } -func (r *Router) join(ctx context.Context, out outbound.Sender, l store.List, sender string) error { +func (r *Router) join(l store.List, sender string) error { if sender == "" { return nil } @@ -90,20 +88,20 @@ func (r *Router) join(ctx context.Context, out outbound.Sender, l store.List, se if ok, err := r.store.IsMember(l.ID, sender); err != nil || ok { return err // members get no mail: it would leak membership } - return r.askConfirmation(ctx, out, l, sender, store.ActionJoin) + return r.askConfirmation(l, sender, store.ActionJoin) } -func (r *Router) leave(ctx context.Context, out outbound.Sender, l store.List, sender string) error { +func (r *Router) leave(l store.List, sender string) error { if sender == "" { return nil } if ok, err := r.store.IsMember(l.ID, sender); err != nil || !ok { return err } - return r.askConfirmation(ctx, out, l, sender, store.ActionLeave) + return r.askConfirmation(l, sender, store.ActionLeave) } -func (r *Router) askConfirmation(ctx context.Context, out outbound.Sender, l store.List, addr, action string) error { +func (r *Router) askConfirmation(l store.List, addr, action string) error { token, created, err := r.store.CreatePending(l.ID, addr, action) if err != nil || !created { return err @@ -124,7 +122,7 @@ func (r *Router) askConfirmation(ctx context.Context, out outbound.Sender, l sto time.Now().Format(time.RFC1123Z), token[:8], strconv.FormatInt(time.Now().UnixNano(), 36), l.Domain, verb, l.Address()) - return out.Send(ctx, l.Name+"+bounce@"+l.Domain, []string{addr}, []byte(msg)) + return r.enqueue([]byte(msg), []store.NewOutbound{{From: l.Name + "+bounce@" + l.Domain, Rcpt: addr}}) } func (r *Router) confirm(l store.List, token string) error { diff --git a/internal/router/m2_test.go b/internal/router/m2_test.go index c1b0e20..5d9f4d5 100644 --- a/internal/router/m2_test.go +++ b/internal/router/m2_test.go @@ -8,6 +8,7 @@ import ( "testing" "codeberg.org/squidd/parsimony/internal/outbound" + "codeberg.org/squidd/parsimony/internal/queue" "codeberg.org/squidd/parsimony/internal/store" ) @@ -34,6 +35,37 @@ func newEnv(t *testing.T) *env { return &env{r: New(s), s: s, out: &outbound.Fake{}, list: l, ctx: context.Background(), alice: ms[0].ID} } +// flush sends everything queued, as the background worker would. +func (e *env) flush() { + w := &queue.Worker{Store: e.s, Sender: e.out} + for { + st, err := w.RunOnce(e.ctx) + if err != nil || st == (queue.Stats{}) { + return + } + } +} + +func (e *env) send(res Result) error { + if err := e.r.Deliver(res); err != nil { + return err + } + e.flush() + return nil +} + +func (e *env) command(sender, rcpt string, data []byte) (bool, error) { + handled, err := e.r.Command(sender, rcpt, data) + e.flush() + return handled, err +} + +func (e *env) release(id int64) error { + err := e.r.Release(id) + e.flush() + return err +} + func lastMail(t *testing.T, out *outbound.Fake) outbound.Sent { t.Helper() if len(out.Sent) == 0 { @@ -48,7 +80,7 @@ func TestSendUsesVERPPerRecipient(t *testing.T) { if err != nil { t.Fatal(err) } - if err := e.r.Send(e.ctx, e.out, res); err != nil { + if err := e.send(res); err != nil { t.Fatal(err) } if len(e.out.Sent) != 2 { @@ -107,7 +139,7 @@ func TestReleaseHeld(t *testing.T) { e.r.Route("stranger@x.com", "dev@example.org", []byte(sample)) held, _ := e.s.ListHeld(e.list.ID) - if err := e.r.Release(e.ctx, e.out, held[0].ID); err != nil { + if err := e.release(held[0].ID); err != nil { t.Fatal(err) } if len(e.out.Sent) != 2 { @@ -119,7 +151,7 @@ func TestReleaseHeld(t *testing.T) { if rest, _ := e.s.ListHeld(e.list.ID); len(rest) != 0 { t.Fatal("released message must leave the queue") } - if err := e.r.Release(e.ctx, e.out, held[0].ID); !errors.Is(err, store.ErrNotFound) { + if err := e.release(held[0].ID); !errors.Is(err, store.ErrNotFound) { t.Fatalf("want ErrNotFound, got %v", err) } } @@ -137,7 +169,7 @@ func token(t *testing.T, m outbound.Sent) string { func TestJoinFlow(t *testing.T) { e := newEnv(t) - handled, err := e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) + handled, err := e.command("carol@x.com", "dev+join@example.org", nil) if !handled || err != nil { t.Fatalf("join: %v %v", handled, err) } @@ -150,14 +182,14 @@ func TestJoinFlow(t *testing.T) { } tok := token(t, m) - handled, err = e.r.Command(e.ctx, e.out, "carol@x.com", "dev+confirm-"+tok+"@example.org", nil) + handled, err = e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil) if !handled || err != nil { t.Fatalf("confirm: %v %v", handled, err) } if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok { t.Fatal("carol must be a member after confirming") } - _, err = e.r.Command(e.ctx, e.out, "carol@x.com", "dev+confirm-"+tok+"@example.org", nil) + _, err = e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil) if !errors.Is(err, ErrBadToken) { t.Fatalf("reused token: want ErrBadToken, got %v", err) } @@ -165,16 +197,16 @@ func TestJoinFlow(t *testing.T) { func TestJoinRateLimitedAndSilentForMembers(t *testing.T) { e := newEnv(t) - e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) - e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) + e.command("carol@x.com", "dev+join@example.org", nil) + e.command("carol@x.com", "dev+join@example.org", nil) if len(e.out.Sent) != 1 { t.Fatalf("second request must not send mail, sent %d", len(e.out.Sent)) } - e.r.Command(e.ctx, e.out, "alice@example.com", "dev+join@example.org", nil) + e.command("alice@example.com", "dev+join@example.org", nil) if len(e.out.Sent) != 1 { t.Fatal("existing members get no mail") } - e.r.Command(e.ctx, e.out, "", "dev+join@example.org", nil) + e.command("", "dev+join@example.org", nil) if len(e.out.Sent) != 1 { t.Fatal("empty sender is ignored") } @@ -183,7 +215,7 @@ func TestJoinRateLimitedAndSilentForMembers(t *testing.T) { func TestJoinClosed(t *testing.T) { e := newEnv(t) e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate) - _, err := e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) + _, err := e.command("carol@x.com", "dev+join@example.org", nil) if !errors.Is(err, ErrJoinClosed) { t.Fatalf("want ErrJoinClosed, got %v", err) } @@ -194,10 +226,10 @@ func TestJoinClosed(t *testing.T) { func TestConfirmAfterListClosed(t *testing.T) { e := newEnv(t) - e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) + e.command("carol@x.com", "dev+join@example.org", nil) tok := token(t, lastMail(t, e.out)) e.s.SetPolicies(e.list.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate) - _, err := e.r.Command(e.ctx, e.out, "carol@x.com", "dev+confirm-"+tok+"@example.org", nil) + _, err := e.command("carol@x.com", "dev+confirm-"+tok+"@example.org", nil) if !errors.Is(err, ErrJoinClosed) { t.Fatalf("want ErrJoinClosed, got %v", err) } @@ -208,7 +240,7 @@ func TestConfirmAfterListClosed(t *testing.T) { func TestLeaveFlow(t *testing.T) { e := newEnv(t) - e.r.Command(e.ctx, e.out, "alice@example.com", "dev+leave@example.org", nil) + e.command("alice@example.com", "dev+leave@example.org", nil) m := lastMail(t, e.out) if m.To[0] != "alice@example.com" { t.Fatalf("to = %v", m.To) @@ -216,13 +248,13 @@ func TestLeaveFlow(t *testing.T) { if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); !ok { t.Fatal("still a member before confirming") } - e.r.Command(e.ctx, e.out, "alice@example.com", "dev+confirm-"+token(t, m)+"@example.org", nil) + e.command("alice@example.com", "dev+confirm-"+token(t, m)+"@example.org", nil) if ok, _ := e.s.IsMember(e.list.ID, "alice@example.com"); ok { t.Fatal("alice must be gone") } // Leaving when not a member sends nothing. n := len(e.out.Sent) - e.r.Command(e.ctx, e.out, "zed@x.com", "dev+leave@example.org", nil) + e.command("zed@x.com", "dev+leave@example.org", nil) if len(e.out.Sent) != n { t.Fatal("non-members get no mail") } @@ -231,9 +263,9 @@ func TestLeaveFlow(t *testing.T) { func TestConfirmTokenFromOtherList(t *testing.T) { e := newEnv(t) e.s.CreateList("ops", "example.org", "Ops") - e.r.Command(e.ctx, e.out, "carol@x.com", "dev+join@example.org", nil) + e.command("carol@x.com", "dev+join@example.org", nil) tok := token(t, lastMail(t, e.out)) - _, err := e.r.Command(e.ctx, e.out, "carol@x.com", "ops+confirm-"+tok+"@example.org", nil) + _, err := e.command("carol@x.com", "ops+confirm-"+tok+"@example.org", nil) if !errors.Is(err, ErrBadToken) { t.Fatalf("want ErrBadToken, got %v", err) } @@ -241,7 +273,7 @@ func TestConfirmTokenFromOtherList(t *testing.T) { func TestCommandNotHandledForPlainAddress(t *testing.T) { e := newEnv(t) - handled, err := e.r.Command(e.ctx, e.out, "a@x.com", "dev@example.org", nil) + handled, err := e.command("a@x.com", "dev@example.org", nil) if handled || err != nil { t.Fatalf("plain address: %v %v", handled, err) } @@ -250,7 +282,7 @@ func TestCommandNotHandledForPlainAddress(t *testing.T) { func TestUnknownCommandAndList(t *testing.T) { e := newEnv(t) for _, rcpt := range []string{"dev+bogus@example.org", "nope+join@example.org"} { - handled, err := e.r.Command(e.ctx, e.out, "a@x.com", rcpt, nil) + handled, err := e.command("a@x.com", rcpt, nil) if !handled || !errors.Is(err, ErrUnknownList) { t.Errorf("%s: %v %v", rcpt, handled, err) } @@ -265,7 +297,7 @@ func TestHardBouncesDisableMember(t *testing.T) { e := newEnv(t) addr := e.r.bounceAddress(e.list, e.alice) for i := 0; i < 3; i++ { - handled, err := e.r.Command(e.ctx, e.out, "", addr, bounceMsg("5.1.1")) + handled, err := e.command("", addr, bounceMsg("5.1.1")) if !handled || err != nil { t.Fatalf("bounce: %v %v", handled, err) } @@ -278,7 +310,7 @@ func TestHardBouncesDisableMember(t *testing.T) { func TestSoftBounceIgnored(t *testing.T) { e := newEnv(t) - e.r.Command(e.ctx, e.out, "", e.r.bounceAddress(e.list, e.alice), bounceMsg("4.2.2")) + e.command("", e.r.bounceAddress(e.list, e.alice), bounceMsg("4.2.2")) if m, _ := e.s.MemberByID(e.alice); m.BounceCount != 0 { t.Fatalf("soft bounce counted: %+v", m) } @@ -287,7 +319,7 @@ func TestSoftBounceIgnored(t *testing.T) { func TestForgedBounceRejected(t *testing.T) { e := newEnv(t) addr := fmt.Sprintf("dev+bounce-%d-000000000000@example.org", e.alice) - _, err := e.r.Command(e.ctx, e.out, "", addr, bounceMsg("5.1.1")) + _, err := e.command("", addr, bounceMsg("5.1.1")) if !errors.Is(err, ErrBadToken) { t.Fatalf("want ErrBadToken, got %v", err) } @@ -301,7 +333,7 @@ func TestBounceFromOtherListRejected(t *testing.T) { e.s.CreateList("ops", "example.org", "Ops") ops, _ := e.s.FindList("ops@example.org") addr := e.r.bounceAddress(ops, e.alice) // valid MAC but alice is not in ops - _, err := e.r.Command(e.ctx, e.out, "", addr, bounceMsg("5.1.1")) + _, err := e.command("", addr, bounceMsg("5.1.1")) if !errors.Is(err, ErrBadToken) { t.Fatalf("want ErrBadToken, got %v", err) } @@ -309,47 +341,81 @@ func TestBounceFromOtherListRejected(t *testing.T) { func TestSystemBounceAddressDiscarded(t *testing.T) { e := newEnv(t) - handled, err := e.r.Command(e.ctx, e.out, "", "dev+bounce@example.org", bounceMsg("5.1.1")) + handled, err := e.command("", "dev+bounce@example.org", bounceMsg("5.1.1")) if !handled || err != nil { t.Fatalf("%v %v", handled, err) } } -// flaky fails for the listed addresses. -type flaky struct { - bad map[string]bool - sent []string -} - -func (f *flaky) Send(_ context.Context, _ string, to []string, _ []byte) error { - if f.bad[to[0]] { - return errors.New("rejected") - } - f.sent = append(f.sent, to[0]) - return nil -} - -func TestSendSkipsFailedRecipients(t *testing.T) { +func TestDeliverQueuesOneEntryPerRecipientAtomically(t *testing.T) { e := newEnv(t) res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample)) - f := &flaky{bad: map[string]bool{"alice@example.com": true}} - if err := e.r.Send(e.ctx, f, res); err != nil { - t.Fatalf("partial failure must not fail the delivery: %v", err) + if err := e.r.Deliver(res); err != nil { + t.Fatal(err) } - if len(f.sent) != 1 || f.sent[0] != "bob@example.com" { - t.Fatalf("sent: %v", f.sent) + if len(e.out.Sent) != 0 { + t.Fatal("Deliver only queues; the worker sends") + } + due, _ := e.s.DueOutbox(10) + if len(due) != 2 { + t.Fatalf("queued = %d", len(due)) + } + if due[0].MsgID != due[1].MsgID { + t.Fatal("recipients must share one stored body") + } + for _, d := range due { + if d.MemberID == 0 || !strings.HasPrefix(d.From, "dev+bounce-") || !strings.Contains(string(d.Prefix), "List-Unsubscribe:") { + t.Fatalf("item: %+v", d) + } + } + body, _ := e.s.OutboxData(due[0].MsgID) + if !strings.Contains(string(body), "List-Id:") || strings.Contains(string(body), "List-Unsubscribe:") { + t.Fatalf("shared body has the common headers only: %q", body) } } -func TestSendFailsWhenNobodyIsReached(t *testing.T) { +func TestDeliverFailsCleanlyWhenTheQueueIsUnavailable(t *testing.T) { e := newEnv(t) res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample)) - f := &flaky{bad: map[string]bool{"alice@example.com": true, "bob@example.com": true}} - if err := e.r.Send(e.ctx, f, res); err == nil { + e.s.Close() + if err := e.r.Deliver(res); err == nil { t.Fatal("want an error so the MTA retries") } } +func TestDeliverWithoutRecipientsQueuesNothing(t *testing.T) { + e := newEnv(t) + woke := 0 + e.r.OnEnqueue = func() { woke++ } + if err := e.r.Deliver(Result{List: e.list, Data: []byte("x")}); err != nil { + t.Fatal(err) + } + if st, _ := e.s.OutboxStats(); st.Pending != 0 || woke != 0 { + t.Fatalf("pending=%d woke=%d", st.Pending, woke) + } +} + +func TestQueuedMailWakesTheSender(t *testing.T) { + e := newEnv(t) + woke := 0 + e.r.OnEnqueue = func() { woke++ } + res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample)) + e.r.Deliver(res) + e.r.Command("carol@x.com", "dev+join@example.org", nil) + if woke != 2 { + t.Fatalf("OnEnqueue called %d times, want once per queued batch (2)", woke) + } +} + +func TestConfirmationMailIsQueuedAsSystemMail(t *testing.T) { + e := newEnv(t) + e.r.Command("carol@x.com", "dev+join@example.org", nil) + due, _ := e.s.DueOutbox(10) + if len(due) != 1 || due[0].Rcpt != "carol@x.com" || due[0].MemberID != 0 || due[0].From != "dev+bounce@example.org" { + t.Fatalf("queue: %+v", due) + } +} + func TestIncomingListHeadersAreStripped(t *testing.T) { r := setup(t) // no BaseURL: One-Click must not survive from the outside msg := "From: Alice \r\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n" + diff --git a/internal/router/m3_test.go b/internal/router/m3_test.go index 6769095..ca43821 100644 --- a/internal/router/m3_test.go +++ b/internal/router/m3_test.go @@ -58,7 +58,7 @@ func TestRejectedAndHeldPostsAreNotArchived(t *testing.T) { t.Fatalf("archived = %d", total) } held, _ := e.s.ListHeld(e.list.ID) - if err := e.r.Release(e.ctx, e.out, held[0].ID); err != nil { + if err := e.release(held[0].ID); err != nil { t.Fatal(err) } if _, total, _ := e.s.ListMessages(e.list.ID, 10, 0); total != 1 { @@ -85,7 +85,7 @@ func sendPost(t *testing.T, e *env) { if err != nil { t.Fatal(err) } - if err := e.r.Send(e.ctx, e.out, res); err != nil { + if err := e.send(res); err != nil { t.Fatal(err) } } @@ -134,7 +134,7 @@ func TestSpoofedUnsubscribeHeadersNeverReachRecipients(t *testing.T) { e := newEnv(t) msg := "From: Alice \r\nList-Unsubscribe: \r\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n\r\nx\r\n" res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(msg)) - e.r.Send(e.ctx, e.out, res) + e.send(res) for _, m := range e.out.Sent { h := header(t, m.Data) if strings.Contains(h.Get("List-Unsubscribe"), "evil") || h.Get("List-Unsubscribe-Post") != "" { diff --git a/internal/router/router.go b/internal/router/router.go index 43d5989..86fc1fc 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -2,16 +2,13 @@ package router import ( - "context" "errors" - "fmt" "log" "strings" "sync" "time" "codeberg.org/squidd/parsimony/internal/archive" - "codeberg.org/squidd/parsimony/internal/outbound" "codeberg.org/squidd/parsimony/internal/store" ) @@ -48,6 +45,8 @@ type Router struct { store *store.Store // BaseURL is the public web URL; enables one-click unsubscribe when set. BaseURL string + // OnEnqueue, when set, is called after mail is queued (to wake the sender). + OnEnqueue func() secretOnce sync.Once secretVal []byte @@ -101,24 +100,30 @@ func (r *Router) prepare(l store.List, data []byte) (Result, error) { return Result{List: l, Recipients: rcpts, Data: out}, nil } -// Send delivers the message to every recipient, each with its own signed -// bounce address (VERP). Individual failures are logged and skipped; an error is -// returned only when nobody could be reached, so a retry cannot duplicate mail. -func (r *Router) Send(ctx context.Context, out outbound.Sender, res Result) error { - failed := 0 +// Deliver queues the message for every recipient, each with its own signed +// bounce address (VERP) and unsubscribe headers. The queue write is atomic: on +// error nothing was queued and the caller may safely retry. +func (r *Router) Deliver(res Result) error { + items := make([]store.NewOutbound, 0, len(res.Recipients)) for _, rc := range res.Recipients { from := r.bounceAddress(res.List, rc.ID) if from == "" { return errors.New("cannot create bounce address") } - data := append(r.unsubscribeHeaders(res.List, rc.ID), res.Data...) - if err := out.Send(ctx, from, []string{rc.Address}, data); err != nil { - failed++ - log.Printf("deliver %s to %s: %v", res.List.Address(), rc.Address, err) - } + items = append(items, store.NewOutbound{ + From: from, Rcpt: rc.Address, MemberID: rc.ID, Prefix: r.unsubscribeHeaders(res.List, rc.ID), + }) } - if failed > 0 && failed == len(res.Recipients) { - return fmt.Errorf("delivery failed for all %d recipients", failed) + return r.enqueue(res.Data, items) +} + +// enqueue stores deliveries and wakes the sender. +func (r *Router) enqueue(data []byte, items []store.NewOutbound) error { + if err := r.store.Enqueue(data, items); err != nil { + return err + } + if r.OnEnqueue != nil && len(items) > 0 { + r.OnEnqueue() } return nil } @@ -156,8 +161,9 @@ func (r *Router) archive(l store.List, data []byte) { } } -// Release approves a held message and delivers it. -func (r *Router) Release(ctx context.Context, out outbound.Sender, heldID int64) error { +// Release approves a held message: it is queued for delivery and leaves the +// moderation queue. +func (r *Router) Release(heldID int64) error { h, err := r.store.GetHeld(heldID) if err != nil { return err @@ -170,7 +176,7 @@ func (r *Router) Release(ctx context.Context, out outbound.Sender, heldID int64) if err != nil { return err } - if err := r.Send(ctx, out, res); err != nil { + if err := r.Deliver(res); err != nil { return err } return r.store.DeleteHeld(heldID) From 98bbcf6b68096b03a3561ce569c372ae8c4d92da Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:54:13 +0100 Subject: [PATCH 6/8] feat(api): inspect and discard the outbound queue - Add GET /api/v1/queue with totals, oldest age and up to 100 deliveries, failing ones first - Add DELETE /api/v1/queue/{id} to discard a stuck delivery - Never expose message contents --- internal/api/api.go | 52 ++++++++++++++++++++++++ internal/api/api_test.go | 87 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/internal/api/api.go b/internal/api/api.go index f4d2e8a..04fad59 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -48,6 +48,8 @@ func New(s *store.Store, token string, rel Releaser) http.Handler { mux.Handle("GET /api/v1/lists/{id}/archive", srv.auth(srv.listArchive)) mux.Handle("GET /api/v1/archive/{id}", srv.auth(srv.getArchived)) mux.Handle("DELETE /api/v1/archive/{id}", srv.auth(srv.deleteArchived)) + mux.Handle("GET /api/v1/queue", srv.auth(srv.getQueue)) + mux.Handle("DELETE /api/v1/queue/{id}", srv.auth(srv.discardQueued)) mux.Handle("GET /api/v1/lists/{id}/held", srv.auth(srv.listHeld)) mux.Handle("POST /api/v1/held/{id}/approve", srv.auth(srv.approveHeld)) mux.Handle("DELETE /api/v1/held/{id}", srv.auth(srv.discardHeld)) @@ -491,3 +493,53 @@ func (s *server) deleteArchived(w http.ResponseWriter, r *http.Request) { } w.WriteHeader(http.StatusNoContent) } + +// Handlers: outbound queue. + +const queueListLimit = 100 + +type queuedDTO struct { + ID int64 `json:"id"` + Recipient string `json:"recipient"` + Attempts int `json:"attempts"` + LastError string `json:"last_error"` + NextAttempt time.Time `json:"next_attempt"` + CreatedAt time.Time `json:"created_at"` +} + +func (s *server) getQueue(w http.ResponseWriter, _ *http.Request) { + st, err := s.store.OutboxStats() + if err != nil { + storeError(w, err) + return + } + items, err := s.store.ListOutbox(queueListLimit) + if err != nil { + storeError(w, err) + return + } + out := make([]queuedDTO, 0, len(items)) + for _, it := range items { + out = append(out, queuedDTO{ID: it.ID, Recipient: it.Rcpt, Attempts: it.Attempts, LastError: it.LastError, + NextAttempt: it.NextAt, CreatedAt: it.CreatedAt}) + } + var oldest *time.Time + if !st.Oldest.IsZero() { + oldest = &st.Oldest + } + writeJSON(w, http.StatusOK, map[string]any{ + "pending": st.Pending, "retrying": st.Retrying, "oldest": oldest, "items": out, + }) +} + +func (s *server) discardQueued(w http.ResponseWriter, r *http.Request) { + id, ok := listID(w, r) + if !ok { + return + } + if err := s.store.CompleteOutbox(id); err != nil { + storeError(w, err) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 0045542..6a68261 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -435,3 +435,90 @@ func TestArchiveGetAndDelete(t *testing.T) { expect(t, do(t, h, "DELETE", "/api/v1/archive/1", "", true), 404) expect(t, do(t, h, "DELETE", "/api/v1/archive/2", "", false), 401) } + +func queueServer(t *testing.T) (http.Handler, *store.Store) { + t.Helper() + s, _ := store.Open(":memory:") + t.Cleanup(func() { s.Close() }) + return New(s, token, nil), s +} + +type queueJSON struct { + Pending int `json:"pending"` + Retrying int `json:"retrying"` + Oldest *time.Time `json:"oldest"` + Items []struct { + ID int64 `json:"id"` + Recipient string `json:"recipient"` + Attempts int `json:"attempts"` + LastError string `json:"last_error"` + NextAttempt time.Time `json:"next_attempt"` + CreatedAt time.Time `json:"created_at"` + } `json:"items"` +} + +func TestQueueEmpty(t *testing.T) { + h, _ := queueServer(t) + rec := do(t, h, "GET", "/api/v1/queue", "", true) + expect(t, rec, 200) + var q queueJSON + decode(t, rec, &q) + if q.Pending != 0 || q.Retrying != 0 || q.Oldest != nil || q.Items == nil || len(q.Items) != 0 { + t.Fatalf("empty queue: %+v (items must be [] not null)", q) + } +} + +func TestQueueShowsPendingAndFailingDeliveries(t *testing.T) { + h, s := queueServer(t) + s.Enqueue([]byte("Subject: x\r\n\r\nx"), []store.NewOutbound{ + {From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}, {From: "b@x.org", Rcpt: "c@x.com"}, + }) + due, _ := s.DueOutbox(10) + s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "connection refused") + + rec := do(t, h, "GET", "/api/v1/queue", "", true) + expect(t, rec, 200) + var q queueJSON + decode(t, rec, &q) + if q.Pending != 3 || q.Retrying != 1 || q.Oldest == nil || len(q.Items) != 3 { + t.Fatalf("queue: %+v", q) + } + if q.Items[0].Recipient != "b@x.com" || q.Items[0].Attempts != 1 || q.Items[0].LastError != "connection refused" { + t.Fatalf("failing deliveries come first: %+v", q.Items[0]) + } + if strings.Contains(rec.Body.String(), "Subject") { + t.Fatal("message contents must not be exposed") + } +} + +func TestQueueListIsCapped(t *testing.T) { + h, s := queueServer(t) + var items []store.NewOutbound + for i := 0; i < queueListLimit+20; i++ { + items = append(items, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)}) + } + s.Enqueue([]byte("x"), items) + var q queueJSON + decode(t, do(t, h, "GET", "/api/v1/queue", "", true), &q) + if q.Pending != queueListLimit+20 || len(q.Items) != queueListLimit { + t.Fatalf("pending=%d items=%d", q.Pending, len(q.Items)) + } +} + +func TestQueueDiscard(t *testing.T) { + h, s := queueServer(t) + s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}}) + due, _ := s.DueOutbox(10) + expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 204) + expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 404) + expect(t, do(t, h, "DELETE", "/api/v1/queue/abc", "", true), 404) + if st, _ := s.OutboxStats(); st.Pending != 1 { + t.Fatalf("pending = %d", st.Pending) + } +} + +func TestQueueRequiresAuth(t *testing.T) { + h, _ := queueServer(t) + expect(t, do(t, h, "GET", "/api/v1/queue", "", false), 401) + expect(t, do(t, h, "DELETE", "/api/v1/queue/1", "", false), 401) +} From f3c75dc97dac630f66d29c674e5f2186bd69fe6b Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:55:52 +0100 Subject: [PATCH 7/8] feat(admin): outbound queue page - Add queue totals, delivery table with errors and next attempt - Refresh on demand and every 5 seconds - Add confirmed discard of a stuck delivery - Link the page from the header --- admin/src/App.tsx | 2 + admin/src/components/Shell.tsx | 11 ++- admin/src/lib/api.test.ts | 10 ++ admin/src/lib/api.ts | 20 ++++ admin/src/pages/QueuePage.test.tsx | 113 +++++++++++++++++++++++ admin/src/pages/QueuePage.tsx | 141 +++++++++++++++++++++++++++++ admin/src/test/fakeApi.ts | 17 +++- docs/openapi.yaml | 32 +++++++ 8 files changed, 341 insertions(+), 5 deletions(-) create mode 100644 admin/src/pages/QueuePage.test.tsx create mode 100644 admin/src/pages/QueuePage.tsx diff --git a/admin/src/App.tsx b/admin/src/App.tsx index c3b4dcb..e832cba 100644 --- a/admin/src/App.tsx +++ b/admin/src/App.tsx @@ -8,6 +8,7 @@ import { ArchiveTab } from '@/tabs/ArchiveTab' import { HeldTab } from '@/tabs/HeldTab' import { MembersTab } from '@/tabs/MembersTab' import { SettingsTab } from '@/tabs/SettingsTab' +import { QueuePage } from '@/pages/QueuePage' import { Toaster } from '@/components/ui/sonner' /** Routes only: providers (query client, router, auth) are set up by the caller. */ @@ -18,6 +19,7 @@ export function App() { } /> }> } /> + } /> }> } /> } /> diff --git a/admin/src/components/Shell.tsx b/admin/src/components/Shell.tsx index d594b00..865716e 100644 --- a/admin/src/components/Shell.tsx +++ b/admin/src/components/Shell.tsx @@ -11,9 +11,14 @@ export function Shell() {
- - Parsimony - +
+ + Parsimony + + + Queue + +
diff --git a/admin/src/lib/api.test.ts b/admin/src/lib/api.test.ts index 28cfc5f..c8622e6 100644 --- a/admin/src/lib/api.test.ts +++ b/admin/src/lib/api.test.ts @@ -149,4 +149,14 @@ describe('endpoints', () => { ['GET', '/api/v1/archive/9'], ]) }) + + it('reads the queue and discards a delivery', async () => { + mockFetch(respond(204)) + await api.queue.discard(4) + await api.queue.get().catch(() => undefined) + expect(calls.map((c) => [c.init.method, c.url])).toEqual([ + ['DELETE', '/api/v1/queue/4'], + ['GET', '/api/v1/queue'], + ]) + }) }) diff --git a/admin/src/lib/api.ts b/admin/src/lib/api.ts index 1686a7a..23666fb 100644 --- a/admin/src/lib/api.ts +++ b/admin/src/lib/api.ts @@ -47,6 +47,22 @@ export interface ArchivePage { messages: Message[] } +export interface QueueItem { + id: number + recipient: string + attempts: number + last_error: string + next_attempt: string + created_at: string +} + +export interface Queue { + pending: number + retrying: number + oldest: string | null + items: QueueItem[] +} + export class ApiError extends Error { readonly status: number constructor(status: number, message: string) { @@ -130,6 +146,10 @@ export const api = { approve: (id: number) => request('POST', `/held/${id}/approve`), discard: (id: number) => request('DELETE', `/held/${id}`), }, + queue: { + get: () => request('GET', '/queue'), + discard: (id: number) => request('DELETE', `/queue/${id}`), + }, archive: { list: (listId: number, p: { q?: string; limit?: number; offset?: number } = {}) => { const qs = new URLSearchParams() diff --git a/admin/src/pages/QueuePage.test.tsx b/admin/src/pages/QueuePage.test.tsx new file mode 100644 index 0000000..4c65104 --- /dev/null +++ b/admin/src/pages/QueuePage.test.tsx @@ -0,0 +1,113 @@ +import { act, screen, waitFor, within } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { QueueItem } from '@/lib/api' +import { installFakeApi, newState } from '@/test/fakeApi' +import { renderApp } from '@/test/render' + +const item = (id: number, over: Partial = {}): QueueItem => ({ + id, + recipient: `user${id}@example.com`, + attempts: 0, + last_error: '', + next_attempt: '2030-01-02T03:04:05Z', + created_at: '2030-01-01T00:00:00Z', + ...over, +}) + +async function setup(items: QueueItem[]) { + const state = installFakeApi(newState({ queue: items })) + const ctx = renderApp('/queue', { token: 'good' }) + await screen.findByRole('heading', { name: /outbound queue/i }) + return { ...ctx, state } +} + +afterEach(() => vi.useRealTimers()) + +describe('queue page', () => { + it('is reachable from the header', async () => { + installFakeApi(newState()) + const { user } = renderApp('/', { token: 'good' }) + await user.click(await screen.findByRole('link', { name: /^queue$/i })) + expect(await screen.findByRole('heading', { name: /outbound queue/i })).toBeInTheDocument() + }) + + it('says when the queue is empty', async () => { + await setup([]) + expect(await screen.findByText(/the queue is empty/i)).toBeInTheDocument() + expect(screen.getByText('Pending').nextElementSibling).toHaveTextContent('0') + }) + + it('shows totals and each delivery, failing ones with their error', async () => { + await setup([ + item(1), + item(2, { attempts: 3, last_error: 'connection refused', next_attempt: '2030-01-02T05:00:00Z' }), + ]) + const failing = (await screen.findByText('user2@example.com')).closest('tr')! + expect(within(failing).getByText('3')).toBeInTheDocument() + expect(within(failing).getByText('connection refused')).toBeInTheDocument() + expect(within(failing).getByText('2030-01-02 05:00 UTC')).toBeInTheDocument() + const fresh = screen.getByText('user1@example.com').closest('tr')! + expect(within(fresh).getByText('—')).toBeInTheDocument() + + expect(screen.getByText('Pending').nextElementSibling).toHaveTextContent('2') + expect(screen.getByText('Retrying').nextElementSibling).toHaveTextContent('1') + expect(screen.getByText('Oldest').nextElementSibling).toHaveTextContent('2030-01-01 00:00 UTC') + }) + + it('refreshes on demand', async () => { + const { user, state } = await setup([item(1)]) + await screen.findByText('user1@example.com') + state.queue = [] + await user.click(screen.getByRole('button', { name: /refresh/i })) + expect(await screen.findByText(/the queue is empty/i)).toBeInTheDocument() + }) + + it('refreshes by itself every few seconds', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) // before the page starts its polling timer + const { state } = await setup([item(1)]) + await screen.findByText('user1@example.com') + state.queue = [] + await act(async () => { + await vi.advanceTimersByTimeAsync(6000) + }) + expect(await screen.findByText(/the queue is empty/i)).toBeInTheDocument() + }) + + it('shows a loading error', async () => { + installFakeApi(newState({ fail: { 'GET /queue': { status: 500, error: 'database is locked' } } })) + renderApp('/queue', { token: 'good' }) + expect(await screen.findByRole('alert')).toHaveTextContent('database is locked') + }) + + describe('discarding', () => { + async function open() { + const ctx = await setup([item(1), item(2)]) + await ctx.user.click(within((await screen.findByText('user1@example.com')).closest('tr')!).getByRole('button', { name: /discard/i })) + return { ...ctx, dialog: await screen.findByRole('alertdialog') } + } + + it('confirms first, naming the recipient; cancel keeps it', async () => { + const { user, state, dialog } = await open() + expect(dialog).toHaveTextContent('user1@example.com') + await user.click(within(dialog).getByRole('button', { name: /cancel/i })) + await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) + expect(state.queue).toHaveLength(2) + }) + + it('discards the delivery', async () => { + const { user, state, dialog } = await open() + await user.click(within(dialog).getByRole('button', { name: /^discard$/i })) + await waitFor(() => expect(screen.queryByText('user1@example.com')).not.toBeInTheDocument()) + expect(state.queue.map((i) => i.id)).toEqual([2]) + expect(screen.getByText('user2@example.com')).toBeInTheDocument() + }) + + it('shows a server error inside the dialog', async () => { + const { user, state, dialog } = await open() + state.fail['DELETE /queue/1'] = { status: 500, error: 'locked' } + await user.click(within(dialog).getByRole('button', { name: /^discard$/i })) + expect(await within(dialog).findByRole('alert')).toHaveTextContent('locked') + expect(state.queue).toHaveLength(2) + }) + }) +}) diff --git a/admin/src/pages/QueuePage.tsx b/admin/src/pages/QueuePage.tsx new file mode 100644 index 0000000..b5d6967 --- /dev/null +++ b/admin/src/pages/QueuePage.tsx @@ -0,0 +1,141 @@ +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { useState } from 'react' +import { toast } from 'sonner' +import { + AlertDialog, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from '@/components/ui/alert-dialog' +import { Button } from '@/components/ui/button' +import { Card, CardContent } from '@/components/ui/card' +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' +import { api, type QueueItem } from '@/lib/api' +import { formatDate } from '@/lib/format' + +/** How often the page reloads by itself. */ +export const REFRESH_MS = 5000 + +export function QueuePage() { + const queue = useQuery({ queryKey: ['queue'], queryFn: api.queue.get, refetchInterval: REFRESH_MS }) + const [discarding, setDiscarding] = useState(null) + const q = queue.data + + return ( +
+
+

Outbound queue

+ +
+ + {queue.isPending &&

Loading…

} + {queue.isError &&

{queue.error.message}

} + + {q && ( + <> +
+ + + +
+ {q.items.length === 0 ? ( +

The queue is empty. Mail is sent as soon as it is accepted.

+ ) : ( + + + + Recipient + Attempts + Last error + Next attempt + Actions + + + + {q.items.map((i) => ( + + {i.recipient} + {i.attempts} + {i.last_error || '—'} + {formatDate(i.next_attempt)} + + + + + ))} + +
+ )} + {q.pending > q.items.length && ( +

Showing {q.items.length} of {q.pending} deliveries.

+ )} + + )} + setDiscarding(null)} /> +
+ ) +} + +function Stat({ label, value }: { label: string; value: string }) { + return ( + + +
{label}
+
{value}
+
+
+ ) +} + +function DiscardDialog({ item, onClose }: { item: QueueItem | null; onClose: () => void }) { + const queryClient = useQueryClient() + const [error, setError] = useState(null) + const discard = useMutation({ + mutationFn: (i: QueueItem) => api.queue.discard(i.id), + onSuccess: async () => { + await queryClient.invalidateQueries({ queryKey: ['queue'] }) + toast.success('Delivery discarded') + onClose() + }, + onError: (e: Error) => setError(e.message), + }) + + return ( + { + if (!open) { + setError(null) + onClose() + } + }} + > + + + Discard this delivery? + + The message for {item?.recipient} will not be sent. Other recipients are not affected. + + + {error && ( +

+ {error} +

+ )} + + Cancel + + +
+
+ ) +} diff --git a/admin/src/test/fakeApi.ts b/admin/src/test/fakeApi.ts index 7fdd8f8..820e9ac 100644 --- a/admin/src/test/fakeApi.ts +++ b/admin/src/test/fakeApi.ts @@ -1,6 +1,6 @@ // An in-memory stand-in for the Parsimony API, used by component tests. import { vi } from 'vitest' -import type { Held, List, Member, Message } from '@/lib/api' +import type { Held, List, Member, Message, QueueItem } from '@/lib/api' export interface FakeState { token: string @@ -8,6 +8,7 @@ export interface FakeState { members: Record held: Record archive: Record + queue: QueueItem[] nextId: number /** Every request seen, as "METHOD /path". */ calls: string[] @@ -16,7 +17,7 @@ export interface FakeState { } export function newState(overrides: Partial = {}): FakeState { - return { token: 'good', lists: [], members: {}, held: {}, archive: {}, nextId: 100, calls: [], fail: {}, ...overrides } + return { token: 'good', lists: [], members: {}, held: {}, archive: {}, queue: [], nextId: 100, calls: [], fail: {}, ...overrides } } export function aList(over: Partial = {}): List { @@ -62,6 +63,18 @@ export function installFakeApi(state: FakeState) { const body = init.body ? JSON.parse(init.body as string) : {} let m: RegExpMatchArray | null + if (path === '/queue' && method === 'GET') { + const items = [...state.queue].sort((a, b) => b.attempts - a.attempts) + const retrying = state.queue.filter((i) => i.attempts > 0).length + const oldest = state.queue.map((i) => i.created_at).sort()[0] ?? null + return json(200, { pending: state.queue.length, retrying, oldest, items }) + } + if ((m = path.match(/^\/queue\/(\d+)$/)) && method === 'DELETE') { + const id = Number(m[1]) + if (!state.queue.some((i) => i.id === id)) return err(404, 'not found') + state.queue = state.queue.filter((i) => i.id !== id) + return json(204) + } if (path === '/lists' && method === 'GET') return json(200, state.lists) if (path === '/lists' && method === 'POST') { if (state.lists.some((l) => l.name === body.name && l.domain === body.domain)) return err(409, 'already exists') diff --git a/docs/openapi.yaml b/docs/openapi.yaml index e5d16b8..52395a0 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -35,6 +35,15 @@ components: date: {type: string, format: date-time} size: {type: integer} body: {type: string, description: Only when reading a single message} + QueueItem: + type: object + properties: + id: {type: integer} + recipient: {type: string} + attempts: {type: integer, description: Failed attempts so far} + last_error: {type: string} + next_attempt: {type: string, format: date-time} + created_at: {type: string, format: date-time} Member: type: object properties: @@ -243,3 +252,26 @@ paths: responses: '204': {description: Deleted} '404': {$ref: '#/components/responses/Error'} + /queue: + get: + summary: Outbound queue totals and up to 100 deliveries (failing ones first) + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + pending: {type: integer} + retrying: {type: integer, description: Deliveries that failed at least once} + oldest: {type: string, format: date-time, nullable: true} + items: {type: array, items: {$ref: '#/components/schemas/QueueItem'}} + /queue/{id}: + parameters: + - {name: id, in: path, required: true, schema: {type: integer}} + delete: + summary: Discard a queued delivery + responses: + '204': {description: Discarded} + '404': {$ref: '#/components/responses/Error'} From 6461e77a95182f1222cca8aaf75380cc59545156 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 16:04:45 +0100 Subject: [PATCH 8/8] feat: document the queue, close mutation gaps, plain relay errors - Document delivery semantics, retries and the queue API in the README and OpenAPI - Add relay.concurrency to the example config - Test that a relay problem never counts against a member, that the longest-waiting delivery goes first, and the error and limit details - Show the plain relay error when there is a single batch --- README.md | 15 +++++++++++++++ admin/src/pages/QueuePage.test.tsx | 14 ++++++++++++++ admin/src/test/fakeApi.ts | 6 ++++-- internal/api/api_test.go | 4 ++-- internal/outbound/outbound.go | 3 +++ internal/outbound/outbound_test.go | 25 +++++++++++++++++++++---- internal/queue/queue_test.go | 21 +++++++++++++++++++++ internal/store/outbox_test.go | 4 ++-- parsimony.example.yaml | 1 + 9 files changed, 83 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 795a466..59daa1d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ A modern, single-binary mailing list manager: an evolution of Mailman. - Postfix delivers to parsimony over LMTP; parsimony relays through SMTP. - Discussion lists with member-only posting, `List-*` headers, one-click unsubscribe and DMARC-safe `From` rewriting. +- Durable outbound queue: a post is accepted once it is stored, one delivery per recipient, + and a background worker sends it with retries (1m, 5m, 15m, 1h, 3h, then every 6h, for up to + 3 days). Relay outages and restarts lose nothing. - Self-service subscribe and unsubscribe by email with double opt-in: send to `list+join@domain` or `list+leave@domain`, then reply to the confirmation. - Moderated lists: posts wait in a queue that the API can approve or discard. @@ -42,6 +45,18 @@ the unsubscribe links in every message use it. Keep the `api` listener private. curl -H "Authorization: Bearer $PARSIMONY_API_TOKEN" -d '{"name":"dev","domain":"example.org","title":"Dev"}' localhost:8080/api/v1/lists ``` +## Delivery and the queue + +LMTP answers `250` when the post is in the queue, so a down relay never bounces mail back to Postfix. +It answers `451` only when the database itself fails. + +- Temporary relay errors (4xx, timeouts, connection failures) are retried with backoff. +- A permanent rejection (5xx) drops that delivery. Only a bad-mailbox reply (`5.1.x`) counts as a hard + bounce for the member; errors such as "relay access denied" never disable members. +- Delivery is at-least-once: a crash between the relay accepting a message and the queue entry being + removed can send one message to one recipient twice. +- `GET /api/v1/queue` and the Queue page show what is pending or failing, and a stuck delivery can be discarded. + ## Admin UI The UI is a single-page app in `admin/`, built into `internal/adminui/dist` and embedded in the binary. diff --git a/admin/src/pages/QueuePage.test.tsx b/admin/src/pages/QueuePage.test.tsx index 4c65104..9922026 100644 --- a/admin/src/pages/QueuePage.test.tsx +++ b/admin/src/pages/QueuePage.test.tsx @@ -35,6 +35,7 @@ describe('queue page', () => { await setup([]) expect(await screen.findByText(/the queue is empty/i)).toBeInTheDocument() expect(screen.getByText('Pending').nextElementSibling).toHaveTextContent('0') + expect(screen.getByText('Oldest').nextElementSibling).toHaveTextContent('—') }) it('shows totals and each delivery, failing ones with their error', async () => { @@ -54,6 +55,19 @@ describe('queue page', () => { expect(screen.getByText('Oldest').nextElementSibling).toHaveTextContent('2030-01-01 00:00 UTC') }) + it('says how many deliveries are not listed when the server caps the list', async () => { + const state = installFakeApi(newState({ queue: [item(1), item(2), item(3)], queueLimit: 2 })) + renderApp('/queue', { token: 'good' }) + expect(await screen.findByText(/showing 2 of 3 deliveries/i)).toBeInTheDocument() + expect(state.queue).toHaveLength(3) + }) + + it('does not mention a cap when everything is listed', async () => { + await setup([item(1), item(2)]) + await screen.findByText('user1@example.com') + expect(screen.queryByText(/showing/i)).not.toBeInTheDocument() + }) + it('refreshes on demand', async () => { const { user, state } = await setup([item(1)]) await screen.findByText('user1@example.com') diff --git a/admin/src/test/fakeApi.ts b/admin/src/test/fakeApi.ts index 820e9ac..4355086 100644 --- a/admin/src/test/fakeApi.ts +++ b/admin/src/test/fakeApi.ts @@ -9,6 +9,8 @@ export interface FakeState { held: Record archive: Record queue: QueueItem[] + /** Like the server, the queue listing is capped. */ + queueLimit: number nextId: number /** Every request seen, as "METHOD /path". */ calls: string[] @@ -17,7 +19,7 @@ export interface FakeState { } export function newState(overrides: Partial = {}): FakeState { - return { token: 'good', lists: [], members: {}, held: {}, archive: {}, queue: [], nextId: 100, calls: [], fail: {}, ...overrides } + return { token: 'good', lists: [], members: {}, held: {}, archive: {}, queue: [], queueLimit: 100, nextId: 100, calls: [], fail: {}, ...overrides } } export function aList(over: Partial = {}): List { @@ -64,7 +66,7 @@ export function installFakeApi(state: FakeState) { let m: RegExpMatchArray | null if (path === '/queue' && method === 'GET') { - const items = [...state.queue].sort((a, b) => b.attempts - a.attempts) + const items = [...state.queue].sort((a, b) => b.attempts - a.attempts).slice(0, state.queueLimit) const retrying = state.queue.filter((i) => i.attempts > 0).length const oldest = state.queue.map((i) => i.created_at).sort()[0] ?? null return json(200, { pending: state.queue.length, retrying, oldest, items }) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 6a68261..32f21ba 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -494,13 +494,13 @@ func TestQueueShowsPendingAndFailingDeliveries(t *testing.T) { func TestQueueListIsCapped(t *testing.T) { h, s := queueServer(t) var items []store.NewOutbound - for i := 0; i < queueListLimit+20; i++ { + for i := 0; i < 120; i++ { items = append(items, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)}) } s.Enqueue([]byte("x"), items) var q queueJSON decode(t, do(t, h, "GET", "/api/v1/queue", "", true), &q) - if q.Pending != queueListLimit+20 || len(q.Items) != queueListLimit { + if q.Pending != 120 || len(q.Items) != 100 { t.Fatalf("pending=%d items=%d", q.Pending, len(q.Items)) } } diff --git a/internal/outbound/outbound.go b/internal/outbound/outbound.go index b53088d..9b1e4fc 100644 --- a/internal/outbound/outbound.go +++ b/internal/outbound/outbound.go @@ -37,6 +37,9 @@ func (r *SMTPRelay) Send(ctx context.Context, from string, to []string, data []b for start := 0; start < len(to); start += size { end := min(start+size, len(to)) if err := r.sendBatch(ctx, from, to[start:end], data); err != nil { + if len(to) <= size { + return err // a single batch: the range adds nothing + } return fmt.Errorf("batch %d-%d: %w", start, end, err) } } diff --git a/internal/outbound/outbound_test.go b/internal/outbound/outbound_test.go index 47b0566..b845890 100644 --- a/internal/outbound/outbound_test.go +++ b/internal/outbound/outbound_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net" + "strings" "sync" "testing" "time" @@ -238,8 +239,8 @@ func TestSendStopsWhenTheContextIsCancelled(t *testing.T) { cancel() select { case err := <-errc: - if err == nil { - t.Fatal("want an error") + if !errors.Is(err, context.Canceled) { + t.Fatalf("want context.Canceled, got %v", err) } case <-time.After(2 * time.Second): t.Fatal("Send ignored the cancelled context while talking to a silent relay") @@ -252,8 +253,8 @@ func TestSendHonoursTheContextDeadline(t *testing.T) { defer cancel() start := time.Now() err := r.Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("x")) - if err == nil { - t.Fatal("want an error") + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("want context.DeadlineExceeded, got %v", err) } if took := time.Since(start); took > 2*time.Second { t.Fatalf("took %v", took) @@ -262,3 +263,19 @@ func TestSendHonoursTheContextDeadline(t *testing.T) { t.Fatalf("a timeout is temporary: %v", err) } } + +func TestSingleBatchErrorsAreNotDecoratedWithBatchNumbers(t *testing.T) { + r := &SMTPRelay{Addr: "127.0.0.1:1"} + err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("x")) + if err == nil || strings.HasPrefix(err.Error(), "batch") { + t.Fatalf("one recipient, one batch: the error should be the plain cause, got %v", err) + } + many := make([]string, 120) + for i := range many { + many[i] = fmt.Sprintf("u%d@x.com", i) + } + err = (&SMTPRelay{Addr: "127.0.0.1:1", BatchSize: 50}).Send(context.Background(), "b@x.org", many, []byte("x")) + if err == nil || !strings.HasPrefix(err.Error(), "batch 0-50:") { + t.Fatalf("with several batches the failing range is useful: %v", err) + } +} diff --git a/internal/queue/queue_test.go b/internal/queue/queue_test.go index 69cc74a..fbddf9b 100644 --- a/internal/queue/queue_test.go +++ b/internal/queue/queue_test.go @@ -240,6 +240,27 @@ func TestBadMailboxCountsAHardBounceAndDisablesAfterThree(t *testing.T) { } } +func TestOtherPermanentRejectionsNeverCountAgainstTheMember(t *testing.T) { + // A misconfigured relay that refuses everything must not empty the list. + e := setup(t) + l, _ := e.s.CreateList("dev", "example.org", "Dev") + e.s.AddMember(l.ID, "ok@x.com", "") + ms, _ := e.s.Members(l.ID) + e.snd.fn = func(context.Context, string, []string) error { + return &smtp.SMTPError{Code: 554, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "relay access denied"} + } + for i := 0; i < 5; i++ { + e.s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "ok@x.com", MemberID: ms[0].ID}}) + st, _ := e.w.RunOnce(context.Background()) + if st.Dropped != 1 || st.Bounced != 0 { + t.Fatalf("round %d: %+v", i, st) + } + } + if m, _ := e.s.MemberByID(ms[0].ID); m.BounceCount != 0 || m.Disabled { + t.Fatalf("member was punished for a relay problem: %+v", m) + } +} + func TestBadMailboxForSystemMailOrUnknownMemberIsHarmless(t *testing.T) { e := setup(t) e.snd.fn = func(context.Context, string, []string) error { diff --git a/internal/store/outbox_test.go b/internal/store/outbox_test.go index 9e88886..4ec14bd 100644 --- a/internal/store/outbox_test.go +++ b/internal/store/outbox_test.go @@ -117,10 +117,10 @@ func TestDueIsOrderedByNextAttempt(t *testing.T) { s.Now = func() time.Time { return now } s.Enqueue([]byte("body"), items(3)) due, _ := s.DueOutbox(10) - s.RetryOutbox(due[0].ID, now.Add(-time.Hour), "e") // oldest next_at + s.RetryOutbox(due[2].ID, now.Add(-time.Hour), "e") // the LAST id, but the longest waiting now = now.Add(time.Minute) got, _ := s.DueOutbox(10) - if got[0].ID != due[0].ID { + if got[0].ID != due[2].ID { t.Fatalf("the longest waiting item goes first: %+v", got) } } diff --git a/parsimony.example.yaml b/parsimony.example.yaml index baea5be..d5c493b 100644 --- a/parsimony.example.yaml +++ b/parsimony.example.yaml @@ -5,6 +5,7 @@ lmtp: addr: 0.0.0.0:2424 relay: addr: postfix:25 + concurrency: 4 # simultaneous deliveries web: listen: 0.0.0.0:8081 # public archive and unsubscribe pages; "" disables them api: