fix(queue): stop putting one deadline on the whole send

Worker.Timeout defaulted to one minute over the entire SMTP
transaction, cutting relays that are slow to accept a message and
causing duplicate deliveries. The default is now no total deadline:
SMTPRelay bounds each phase itself. A positive Timeout still applies.
This commit is contained in:
tgiachi 2026-11-10 15:20:49 +01:00
commit af487b8c84
2 changed files with 25 additions and 7 deletions

View file

@ -20,7 +20,6 @@ const MaxAge = 72 * time.Hour
const (
defaultConcurrency = 4
defaultBatch = 50
defaultTimeout = time.Minute
defaultInterval = 5 * time.Second
// purgeEvery is the least time between two scans that forget old failed deliveries.
@ -55,7 +54,7 @@ type Worker struct {
Sender outbound.Sender
Concurrency int // simultaneous sends
Batch int // deliveries taken per run
Timeout time.Duration // per send
Timeout time.Duration // per send; zero leaves the deadlines to Sender (SMTPRelay bounds each SMTP phase)
Interval time.Duration // idle polling, to pick up retries that have become due
// Sign, when set, signs the final message (DKIM). On error the message is sent unsigned:
// not delivering is worse than a missed signature.
@ -245,15 +244,16 @@ func (w *Worker) deliver(ctx context.Context, cache *bodies, it store.OutboxItem
}
}
timeout := w.Timeout
if timeout <= 0 {
timeout = defaultTimeout
}
// Wait for the slot before starting the timeout, so waiting does not eat into the send.
if err := w.pace().Wait(ctx); err != nil {
return Stats{} // shutting down: the delivery stays queued, unchanged
}
sctx, cancel := context.WithTimeout(ctx, timeout)
// No total deadline by default: one that covers the whole transaction cut relays that take
// minutes to accept a message, which was then retried and delivered twice.
sctx, cancel := ctx, context.CancelFunc(func() {})
if w.Timeout > 0 {
sctx, cancel = context.WithTimeout(ctx, w.Timeout)
}
err = w.Sender.Send(sctx, it.From, []string{it.Rcpt}, msg)
cancel()

View file

@ -405,6 +405,24 @@ func TestSlowRelayTimesOutAndIsRetried(t *testing.T) {
}
}
func TestTheQueueLeavesTheSendDeadlinesToTheSender(t *testing.T) {
// One total deadline cut a relay that took over a minute to answer the final dot: the message
// was accepted, then retried and delivered again. The sender bounds each SMTP phase itself.
e := setup(t)
e.enqueue(t, 1)
var hasDeadline bool
e.snd.fn = func(ctx context.Context, _ string, _ []string) error {
_, hasDeadline = ctx.Deadline()
return nil
}
if st, _ := e.w.RunOnce(context.Background()); st.Sent != 1 {
t.Fatalf("sent: %+v", st)
}
if hasDeadline {
t.Fatal("the queue must not put a deadline on the whole send")
}
}
func waitFor(t *testing.T, what string, cond func() bool) {
t.Helper()
deadline := time.Now().Add(3 * time.Second)