Skip to content

v1.1.0 — high-throughput dispatcher rewrite

Latest

Choose a tag to compare

@bravo1goingdark bravo1goingdark released this 28 Apr 08:36

Highlights

This release reworks the dispatcher hot path so bulk campaigns no longer fsync per send, re-encode attachments per recipient, or hold every rendered body in RAM.

Performance

  • Coalesced offset saves. Per-success disk writes are replaced with an in-memory contiguous high-water mark plus a 200ms flusher goroutine. Integration test shows ~2500× fewer disk saves under 16-worker concurrency. Microbench: MarkComplete 33 ns/op vs the old UpdateOffset+Save at 28 µs/op (842×).
  • Per-dispatch attachment cache (email.AttachmentCache). A 5 MB PDF sent to 1M recipients is now read & base64-encoded once, not 1M times. Size-capped at 256 MiB by default with a streaming fallback.
  • Streaming task pipeline. cli.StreamEmailTasks + email.StartDispatcherStream let the bulk path render and send concurrently. Memory drops from O(recipients × body) to O(buffer). The CLI uses streaming automatically; dry-run keeps the slice path.
  • Bulk monitor seed. New monitor.Monitor.InitializePending([]string) registers all recipients in one lock acquisition instead of N per-recipient UpdateRecipientStatus calls.
  • Misc. Fixed-order MIME header writes (no concat allocs, stable for DKIM), crypto/rand MIME boundaries, wg.Add(concurrency) once, pre-allocated task slice.

Correctness fixes

  • Resume under concurrency now persists a contiguous high-water mark. The previous code persisted max(idx), so if worker B finished idx 50 while worker A was still on idx 30, the saved offset would be 51 — and on a crash + resume, idx 30 would be skipped. Fixed via OffsetTracker.MarkComplete(idx).
  • Fixed StartOffset + Index + 1 double-count. On resume, the saved offset would jump by twice the resume baseline. Removed.
  • Skipped recipients no longer stall the high-water mark. Index is now sequential post-skip, not raw recipient position.

Caveat

Resume is correct under concurrency. It is not guaranteed correct under input mutation between runs — if a CSV row becomes invalid (or vice versa) before a resume, positional offset can shift. This was true before and remains so.

New / changed public API

  • email.StartDispatcherStream(ctx, taskCh <-chan Task, ...) — channel-based dispatch
  • email.AttachmentCache, NewAttachmentCache(maxBytes int64)
  • email.OffsetTracker.MarkComplete(idx int) — interface change; external implementers of OffsetTracker must add this method
  • email.DispatchOptions.AttachmentCache, OffsetSaveInterval, PendingEmails
  • monitor.Monitor.InitializePending(emails []string) — interface change; external implementers must add this method
  • cli.StreamEmailTasks(...) (<-chan email.Task, <-chan error)

Tests

5 new MarkComplete tests (in-order, out-of-order, duplicate-below-offset, resume baseline, 1000-goroutine concurrent), 4 new AttachmentCache tests, 2 new flusher integration tests, plus benchmarks. Full suite passes under go test -race ./....

Full diff: v1.0.0...v1.1.0