Skip to content

Lifecycles ​

Four things in amqp-contract have a life: a client, a worker, an RPC call, and a delivery. Each is one state at a time, and each state is one named value in the source — the diagrams below are those types, not a summary of them.

Client ​

TypedAmqpClient.create() starts in connecting and answers once the state has left it.

StateHow you get therepublish() / call()isConnected()
connectingcreate() is pendingyou have no instance yet—
setup-failedcreate() answers Defect(TechnicalError), after closing ityou have no instance—
readycreate() answered Ok(client)sent; buffered up to publishTimeoutMs while the connection is downthe connection's own
channel-lostthe broker closed the channel and the connection stayed upbuffered up to publishTimeoutMs, then Err(PublishError); nothing is consumedfalse
closedclose(), or a create() that failed (Err(ConnectionError) on a timeout)Err(PublishError), reason "channel-closed", at oncefalse

The broker closes a channel for a protocol error (a 406, a publish to a missing exchange) or a handler that outlives consumer_timeout. Usually the connection goes down with it and is re-established, channel included — the client stays ready throughout. channel-lost is the other case: something listens for the channel's error event (a channelOptions.setup that logs it, say), so the connection survives with no channel on it, and amqp-connection-manager opens a channel only when the connection (re)connects. The client logs it at error and reconnects the connection itself — after 1 second, doubling up to 30 while new channels keep being closed. Every channel on that connection reconnects with it.

ready covers an outage: amqp-connection-manager reconnects on its own, isConnected() reports false meanwhile, and the client stays ready. closed is terminal — a closed client is never reopened, and a second close() returns the first one's result. Create a new client instead.

A failed create() never hands you an instance: it closes what it built first, so there is no pooled connection to release.

RPC call ​

One client.call() is one entry, keyed by its correlationId, from the moment the call starts until it settles.

The entry exists before the request is validated and published, so a reply that races the publisher confirm still finds it. timeoutMs and close() reach a call in either state: a request still buffered behind an outage, or a reply whose schema validates slowly, does not extend the budget and does not outlive close(). A request still buffered when its call times out is dropped from the buffer, not published when the connection returns. One written to the broker before the deadline carries what was left of the budget as its expiration, so it expires when the call does; a reply that arrives after the call ended is counted as late. A call settles once; a second reply for the same correlationId is logged as late and counted on amqp.client.rpc.late_reply.

See client-side RPC errors for each error.

Worker ​

close() is safe to call twice — from a signal handler and a finally, say. Every call waits for the same consumer cancellation, so none of them closes the channel while the broker can still deliver. Each call then drains with its own drainTimeoutMs, which is how a later call shortens the wait of an earlier one. See shut down without dropping messages.

Delivery ​

Every delivery ends in exactly one Outcome, and the outcome alone decides how the delivery is settled.

dead-lettered means nack(requeue: false): the queue's dead-letter exchange receives the message, or — on a queue declared onPoison: "drop" — the broker discards it. The retry model explains why each branch goes where it does.

Where the states live ​

LifecycleTypeSource
ClientLifecyclepackages/core/src/amqp-client.ts
RPC callPendingCall["phase"]packages/client/src/client.ts
WorkerTypedAmqpWorker's lifecyclepackages/worker/src/worker.ts
DeliveryOutcomepackages/worker/src/outcome.ts
Retry routingRetryActionpackages/worker/src/retry.ts

All five are internal: read them to understand the library, do not depend on them.

Where next ​

Released under the MIT License.