Execution Pipeline
Pipeline Stages
A transport never calls a handler directly. Every delivery goes through the same three stages, shared by LocalBus, NATS Core and JetStream:
Decode → InvokeHandler (chain, HandlerDone) → Settle (Dispose → broker action)
- Decode turns the received bytes into an event. A decode failure has no event and no handler run: it goes straight to Settle.
- InvokeHandler (
event.InvokeHandler) runs the invoker chain around the handler and never lets a panic escape the calling goroutine. A panic in the handler is recovered innermost, so the chain sees it as an ordinary*event.PanicError. It emitsHandlerDoneto the Observer with the outcome, the duration and the error. - Settle (
event.Settle) is called exactly once per handled message. It maps the result throughevent.Disposeto a disposition (Ack, Retry, RetryDelayed or Terminal), dead-letters a terminal failure through the busDLQPublisherwhen the chain did not already do so, and reports the terminal outcome once throughObserver.Failure. The transport then performs the broker action for the disposition. The per-error results are in the failure model.
Invoker Chain Depth
Handlers are never executed directly. Inside InvokeHandler, execution flows through a chain of invokers:
Invoker → Invoker → ... → Handler
Each invoker has a single responsibility and is stateless or externally backed. The chain uses recursive function composition: each invoker receives a next function that calls the next invoker, until the final handler is reached.
Recommended Order
Tracer → RateLimit → Idempotency → DLQ → Retry → CircuitBreaker → Handler
The reason for each position is on the Composition page.
Context Flow
Context flows through the chain and can be enriched at each layer:
- The transport installs the Observer, the
Delivery(count, maximum, subject) and theExecutionon the handler context - Tracing adds span context
- The handler receives the final enriched context
Handler contexts derive from the context the transport was given (the Subscribe context on NATS Core and JetStream, the emit context on LocalBus), never from context.Background(), so cancellation and span parenting reach the handler.
Error Flow
Errors propagate back through the chain:
- Each invoker can inspect, classify, or wrap errors
Retryre-executes on transient errors and gives up with an error that matchesErrRetriesExhaustedDLQpublishes the final outcome and returns an error that matchesErrDeadLettered, which the transport acknowledges- What leaves the chain is settled by the transport: a terminal outcome is reported once through
Observer.Failure, and then through the event’s ownOnErrmethod when the event implements it