Skip to main content

Crate cellos_sink_jetstream

Crate cellos_sink_jetstream 

Source
Expand description

JetStream cellos_core::ports::EventSink — publishes JSON payloads to a configured subject.

§Reconnect / circuit-breaker model (P3-01)

JetStreamEventSink keeps a small in-memory state machine in front of the underlying NATS connection so an extended broker outage cannot stall the supervisor’s emit() path:

  • Connected — last publish succeeded. Next emit() calls [Publisher::publish] directly (with a small bounded retry, see with_retry).
  • Reconnecting { attempt, next_after } — last publish failed. emit() returns Err immediately while Instant::now() < next_after (the circuit is “open”). Once next_after has elapsed the sink takes one bounded probe attempt; on success the state resets to Connected, on failure attempt is incremented and next_after is pushed forward by an exponentially-backed-off interval.

The async-nats Client performs its own TCP-level reconnect under the hood — this state machine is layered on top so that the supervisor’s emit() returns Err quickly during an outage instead of being held by the underlying client’s blocking publish path. Operators decide what to do with that Err (typically log and continue).

Backoff schedule: 100ms * 2^attempt with attempt saturating at the cap that keeps the value below 3 minutes (180s). So the sequence is 100ms, 200ms, 400ms, 800ms, 1.6s, 3.2s, …, 102.4s, 180s, 180s, …. Backoff resets to attempt = 0 on the first successful publish after an outage (acceptance: “Backoff resets on successful publish”).

Structs§

JetStreamEventSink
Publishes serialized CloudEventV1 to JetStream.

Constants§

TENANT_ID_DEFAULT_TOKEN
Sentinel value substituted for {tenantId} when the event carries no tenant.
TENANT_ID_PLACEHOLDER
Subject template placeholder for the per-tenant isolation dimension (A2-03).

Functions§

resolve_tenant_subject
Resolve a JetStream subject for event against template.
with_retry
Run f up to max_attempts times with exponential backoff on failure.