Publishes delivered hooks to a NATS JetStream subject. The value is
Ankusa.Sink.Message, byte-identical to what Ankusa.Sink.RabbitMQ and
Ankusa.Sink.Kafka publish: inline up to :inline_max_bytes, a claim
ticket above it.
The subject the hook lands on — not the stream — is the sink's business, and
it belongs to whoever operates the stream: the stream's configured subjects
decide whether the publish is stored at all. So this sink never creates
or updates a stream. A stream's storage, retention, replicas, and subject
set are an operator's capacity and ordering contract, not something the
first hook to arrive should decide. A subject no stream covers is a
{:error, :no_stream} that flows into the source's
Ankusa.RetryPolicy, then the DLQ — never a silently auto-created stream.
(Compare Ankusa.Sink.Kafka, which likewise never creates its topic.)
Publishing is confirmed
deliver/3 returns :ok only after JetStream's own publish
acknowledgement — the {"stream": ..., "seq": ...} the server sends back
once the message is committed to the stream. It is not "the bytes reached a
socket": the publish is a NATS request with a reply inbox, and the reply is
awaited (bounded by :publish_timeout_ms, default 5s), the equivalent of
RabbitMQ's publisher confirms and Kafka's acks=all. Everything else is an
{:error, reason} that dispatch retries, then dead-letters: :publish_timeout
when the ack never arrives, :no_stream when no stream covers the subject,
and {:jetstream, %{"code" => 400, "description" => "message size exceeds maximum allowed"}} for a publish the stream itself refuses (size, TTL,
permissions). The one exception is {:permanent, :max_payload_exceeded}: a
publish larger than the server's max_payload is refused by gnat before it is
sent, and is dead-lettered at once.
A message over :max_message_bytes (1 MiB by default) is refused before the
connection is used, with no server call: an inline body whose base64 does not
fit goes through the claim check instead, and a body that is over the limit by
itself is {:error, {:permanent, {:message_too_large, size, max}}} (see
Ankusa.Sink.Message).
A publish whose ack is lost can still have been stored, so delivery is
at-least-once. Every publish sets Nats-Msg-Id to the hook's id — or
id:replay:<replay_id> for a replay — so JetStream's duplicate window
collapses a lost-ack retry of one delivery, while a deliberate replay of the
same hook carries a distinct id and is always stored. Consumers dedupe on
the message's idempotency_key.
What each message carries
- subject —
:subject(a static string, or a 1-arity fun over theAnkusa.Envelope). - headers —
Nats-Msg-Id(id, orid:replay:<replay_id>on a replay), thenAnkusa.Sink.Message.headers/2:ankusa_id,ankusa_source_id,ankusa_tenant_id,ankusa_message_version,ankusa_idempotency_key,content_type— the same setAnkusa.Sink.Kafkasets, plusankusa_dedupe_keyandankusa_replay_idwhen the hook carries them — so a consumer parses one set regardless of transport. - body — the
Ankusa.Sink.MessageJSON.
Process model
The first deliver/3 for an {instance, connection} pair starts a
Gnat.ConnectionSupervisor under this package's DynamicSupervisor. The
start returns at once: the NATS handshake runs in that supervisor, never in
the caller or the DynamicSupervisor, so a slow or dead server cannot stall
other deliveries behind it. It connects to one of :servers, picked at
random per attempt (gnat's behaviour), and reconnects with a 2 s backoff
whenever the socket closes.
Both processes are found through the instance's registry
(Ankusa.via(instance, {:nats_conn, connection})): nothing is registered
globally, and two instances never share a connection. While the connection
is not up — the first delivery, a reconnect — deliver/3 returns
{:error, :not_connected}, retried by the source's Ankusa.RetryPolicy
like any other sink failure. Servers and credentials are fixed by the first
delivery that starts it; use a different :connection for a different
cluster.
opts
:servers— required;"host:port"strings or{host, port}tuples. More than one is failover, not fan-out: one connection to one server (picked at random per attempt) at a time.:subject— required; a string, or(Envelope.t() -> String.t()):inline_max_bytes— default 64 KiB (65,536), configurable:max_message_bytes— the largest message published; default`1_048_576`, NATS's default `max_payload` (1 MiB). A message over it never reaches the server: an inline body whose base64 does not fit is checked in instead, and a body that is over it by itself is `{:error, {:permanent, {:message_too_large, size, max}}}`. Raise it with the server's `max_payload`:publish_timeout_ms— how longdeliver/3waits for the JetStreampublish ack; default `5_000`:connection— atom naming the gnat connection; default:default:idle_timeout_ms— close the connection after this long without adelivery through it, default `600_000` (10 min); `0` never. Never under `:publish_timeout_ms` plus 1 s. See `Ankusa.Sink.Reaper`:client_name— the name this client reports to NATS monitoring:tls,:ssl_opts,:tcp_opts,:connection_timeout,:ping_interval,:inbox_prefix— passed to gnat's connection settings- credentials —
:username(needs:password),:password,`:token`, or `:nkey_seed` (with `:jwt` for operator-mode accounts); each secret is handed to gnat as a zero-arity function, so no gnat process holds it in the clear. gnat sends exactly one scheme, choosing in that order, so set one.
:no_responders is always on. It costs nothing when the server answers, and
it turns "no stream covers this subject and nothing else is listening" into
an immediate {:error, :no_stream} rather than a :publish_timeout_ms
hang.