gobridge

0017 — direct_hold retries a failed send in process before replay or dead-lettering

Status: accepted Date: 2026-09-22 Deciders: GoBridge core Relates to: 0015 (a redrive whose inject fails only temporarily is dead-lettered again less often), 0004 (a wedged route stops the loop) Amended by: 0019 (each entry of an admin redrive batch has its own bound)

Context

A direct_hold route holds the source message unacknowledged while it sends to the destination, and settles the source only once the send has returned. When that send failed with a recoverable error, the route never tried the send again itself. One failed send led straight to one decision:

Which of the two applied depended on whether the bridge could count how many times it had already seen that message:

What operators saw was that a destination which was unavailable for a few seconds — a queue policy still propagating, a broker restart, a throttling burst — either emptied into the dead-letter store, one manual redrive per message, or recycled the MQTT session over and over.

Nothing else covered that gap. The SQS sender inherits the AWS SDK’s own retryer, which absorbs a few seconds; the MQTT and AMQP senders have no such layer at all.

The route setting replay_budget (default 15 minutes) reads like a retry budget for these routes. It is not one: only the shared_outbox drainer reads it, and a direct_hold route never has.

Decision

A direct_hold route retries a recoverable send inside the bridge, with backoff, for a bounded time, before the decision above runs. The source message stays held and unacknowledged for the whole time, so nothing is acknowledged that was not delivered and nothing is dead-lettered that a short outage would have cured.

The loop. Each pass is one physical send under its own send_timeout, and keeps the per-send wedge ceiling it always had. Between two sends the route waits on the same backoff ladder it uses everywhere else: a RetryAfter hint from the destination (SQS throttling, for example) replaces the backoff step and is not jittered, and otherwise the route’s backoff applies — 1 s doubling to 30 s with jitter, by default. Either way the wait is never shorter than the 100 ms floor described next. With the default backoff and no hint, the first wait is therefore about one second.

No wait is shorter than 100 ms. A shorter backoff interval or RetryAfter hint is raised to that floor before the budget is asked whether it can cover the wait, so a budget too short for even the raised first wait is spent at once. The floor caps one delivery at budget ÷ 100 ms sends — about 600 at the 60-second default — where a legal initial_interval: 1ms with multiplier: 1 would otherwise mean some 60,000 sends, and a jittered nanosecond interval a CPU-bound loop against the destination. It never binds under the default backoff, and it is a floor on the pace only: the stop conditions below are unchanged.

It stops on the first of:

All of those are re-checked when a wait ENDS, not only right after a send. The loop spends almost all of its budget parked on a backoff timer, and its state can change during that park: another delivery can latch the wedge, and the bridge can cancel the delivery context. The budget is re-measured there too, because a timer guarantees a MINIMUM delay and nothing more — scheduler pressure or a GC pause can resume the loop past the budget the delay was measured against, and the two validation rules below are sized on the last send STARTING inside the budget. A delivery that wakes into any of these stops without a further physical send.

Afterwards nothing changes. The same replay-cap gate runs and the message is either handed back to its source or written to the dead-letter store exactly as before. In-process retries do not spend the message’s replay budget: the bridge-owned replay ledger is still charged once per delivery, however many sends that delivery took.

The budget is a new route policy field, send_retry_budget (routing.RoutePolicy.SendRetryBudget). It applies to direct_hold only, the mirror of replay_budget being read by the drainer only.

Hooks and metrics. OnAttempt and the OnDelivery callback fire once per physical send, so a hook sees every one of them and their errors. Every physical send of one delivery reports the same DeliveryAttempt.Attempt: that number is the delivery-level attempt that max_replay_attempts caps, and in-process retries do not advance it. OnSettled still fires exactly once.

Two counters, both tagged route_id and both emitted only while the budget is enabled:

Metric Meaning
SendRetries One per in-process retry after a recoverable send failure.
SendRetryBudgetExhausted One per held send whose retries used up the budget.

RouteErrors now counts a failed send once the in-process retry has given up, not on the first failed send. A stalled destination therefore shows up in RouteErrors up to the budget plus the last send’s hold after the first send started — that hold is at most one send wedge ceiling (defined below), so 95 seconds with the defaults — or never, when the destination recovers inside the budget. SendRetries is the early signal to alert on instead.

A waiting delivery keeps its slot. It holds its route’s max_in_flight slot — and the runtime-wide in-flight slot, where a runtime-wide limit is configured — for the whole wait, so intake slows down while a destination is unwell. That is the intended effect: the source becomes the buffer instead of the dead-letter store. A persistent MQTT session’s broker keeps queueing for the session, an SQS message stays invisible under auto-extend, and an AMQP link runs out of credit. The cost is stated plainly: one route whose destination is down can hold runtime-wide in-flight capacity for up to the budget, which slows unrelated routes sharing it.

Two validation rules keep the hold inside what the source will tolerate. Both run when the configuration is loaded, so a bad combination never reaches production traffic. Both size the last physical send by the send wedge ceiling, send_timeout + min(send_timeout, 5s): a sender that ignores its context keeps the delivery that long before the route gives up on it and wedges. The validator and the dispatch path read the same function, route.SendWedgeCeiling, so the two cannot drift apart. The ceiling applies to direct_hold routes only, because only they send while the source is still held. Every other delivery mode settles its source before it sends — a shared_outbox route acks once the outbox record is persisted — so its fixed-window sum keeps plain send_timeout, unchanged.

  1. On a source with a fixed visibility window — an SQS queue without auto-extend — the budget is counted into the worst case the message may spend before it is settled: processors × processor_timeout + send_retry_budget + send wedge ceiling + DLQ budget ≤ visibility timeout. Over that, the source redelivers mid-pipeline and the message is processed twice. The rejection reads worst-case pipeline time (… + SendRetryBudget … + SendTimeout … (+… wedge grace) + DLQ budget …) exceeds source VisibilityTimeout (…); source may redeliver mid-pipeline causing duplicate processing (lower send_retry_budget, set it to 0s to turn in-process send retry off, or auto-extend the source window). A source that auto-extends its window is skipped, as it always was.
  2. On a source session that recycles its broker connection to recover stranded settlements — an MQTT persistent or exclusive session — that recycle first waits a bounded time for the deliveries the runtime already accepted to settle. A held retry still running when the wait runs out fails the recovery attempt and terminalizes the session. The last send starts just inside the budget and may then hold the delivery until its send wedge ceiling, so send_retry_budget + send wedge ceiling is the hold the wait has to cover. The rejection reads send_retry_budget … + send_timeout … (+… wedge grace) exceeds the source's settlement-recovery wait …; a held retry would outlive the wait and fail the source's recycle (lower send_retry_budget or send_timeout, or raise the source session's settlement-recovery wait through its transport's own timeouts). For an MQTT session those timeouts are its connect and reconcile timeouts.

The session reports that wait through a new optional typed-config capability, ports.SettlementRecoveryTimingConfig, which the MQTT transport implements from the same numbers its own recycle uses. The validator and the adapter therefore cannot disagree about how long a held delivery may take to settle.

The second rule is necessary, not sufficient. The recovery wait is the outer deadline for the whole recovery attempt, not a budget reserved for settling: the same 300 seconds also covers waiting for the session serialization gate, the teardown drain, and the disconnect, reconnect and reconcile that follow it. And one held delivery can occupy more of that wait than send_retry_budget + send wedge ceiling: the message runs its processor chain first, where each processor may take up to processor_timeout, and a message that ends up dead-lettered spends a further 10.5 seconds on that write. With the shipped defaults and no processors, the worst case one delivery can hold grew from about 45 seconds to about 105 seconds of the same 240. The rule catches the obvious overrun, and a long processor chain plus a dead-letter write can still crowd the recycle on a route that passes it. The cure is a smaller send_retry_budget, or larger session connect_timeout / reconcile_timeout values, which is what the wait is computed from.

Consequences

Alternatives considered