pub struct NatsClient {
pub client: Client,
pub service: Arc<Service>,
pub(crate) jetstream: Context,
pub(crate) service_name: String,
pub(crate) signals: Arc<ConnectionSignals>,
pub(crate) stream_config: MessageBusConfig,
}Expand description
Live handle to a NATS client including a JetStream connection.
Implements Publisher,
Subscriber,
Requester, and
Responder.
Obtain one via NatsConfiguration::connect.
The cheap-to-clone SharedNatsClient alias
(Arc<NatsClient>) is the type passed around in application code.
Fields§
§client: ClientNATS connection.
service: Arc<Service>NATS Service for the component that owns this connection.
Having a NATS service allows us to use NATS to see which components are online, what version they’re using, and do additional introspection in the long term (currently only pings work).
jetstream: ContextJetStream context, used by the
Publisher,
Subscriber,
Requester, and
Responder implementations.
service_name: StringDurable consumer name; instances sharing the same name load-balance automatically.
signals: Arc<ConnectionSignals>Shared reconnect signals, also held by the connection’s event callback.
stream_config: MessageBusConfigMessage bus configuration to create different kinds of streams.
Implementations§
Source§impl NatsClient
impl NatsClient
Sourceconst MAINTENANCE_MODE_KEY: &str = "maintenance_mode"
const MAINTENANCE_MODE_KEY: &str = "maintenance_mode"
Subject for the maintenance mode.
Maintenance mode is considered enabled if its value is 1 or unset, disabled if zero.
Sourcepub async fn set_maintenance_mode(
&self,
maintenance_mode: bool,
) -> Result<(), NatsError>
pub async fn set_maintenance_mode( &self, maintenance_mode: bool, ) -> Result<(), NatsError>
Sets maintenance mode to the boolean value
Sourcepub async fn is_in_maintenance_mode(&self) -> Result<bool, NatsError>
pub async fn is_in_maintenance_mode(&self) -> Result<bool, NatsError>
Returns true if the NATS K/V store says we are in maintenance mode
Source§impl NatsClient
impl NatsClient
Sourcepub async fn make_nats_request<T: Serialize>(
&self,
subject: impl ToSubject,
request: T,
) -> Result<Subscriber, NatsError>
pub async fn make_nats_request<T: Serialize>( &self, subject: impl ToSubject, request: T, ) -> Result<Subscriber, NatsError>
Make a request over JetStream, and create a subscriber that’ll wait for a response over NATS
Sourcepub async fn await_nats_response<U: DeserializeOwned, E: DeserializeOwned>(
subscription: Subscriber,
) -> Result<Result<U, E>, NatsError>
pub async fn await_nats_response<U: DeserializeOwned, E: DeserializeOwned>( subscription: Subscriber, ) -> Result<Result<U, E>, NatsError>
Given a subscriber created using NatsClient::make_nats_request(), get the response.
Source§impl NatsClient
impl NatsClient
Sourcepub async fn get_roomserver_stream(&self) -> Result<Stream, NatsError>
pub async fn get_roomserver_stream(&self) -> Result<Stream, NatsError>
Get the stream for the roomserver
Sourcepub async fn set_up_streams(&self) -> Result<(), NatsError>
pub async fn set_up_streams(&self) -> Result<(), NatsError>
Sets up all required streams in NATS
Source§impl NatsClient
impl NatsClient
Sourcepub async fn notify_new_sync_data(&self) -> Result<(), NatsError>
pub async fn notify_new_sync_data(&self) -> Result<(), NatsError>
Notifies clients that there is potentially some new data to be fetched in the sync endpoint.
NOTE: This is intentionally using a NATS core stream because there is no retention/acknowledgment requirements on that stream.
Sourcepub async fn sync_data_subscriber(&self) -> Result<Subscriber, NatsError>
pub async fn sync_data_subscriber(&self) -> Result<Subscriber, NatsError>
Subscribes to the sync update subject to get notified if something changed.
Source§impl NatsClient
impl NatsClient
Sourcepub(crate) fn internal_channel_size(&self) -> usize
pub(crate) fn internal_channel_size(&self) -> usize
Buffer size for the internal channel between a subscription’s background
forward task and its consumer-facing stream. See
ConsumerConfig::buffer_size.
Sourcepub async fn connect_to_store(jetstream: &Context) -> Result<Store, NatsError>
pub async fn connect_to_store(jetstream: &Context) -> Result<Store, NatsError>
Given a JetStream context, returns a key-value store
Sourcepub(crate) async fn ensure_event_stream<E: Event>(
&self,
) -> Result<Stream, CreateStreamError>
pub(crate) async fn ensure_event_stream<E: Event>( &self, ) -> Result<Stream, CreateStreamError>
Ensures the JetStream event stream for E exists and returns its
handle.
Shared by Publisher::publish
(which discards the handle, just ensuring the stream exists) and
Subscriber::subscribe (which
uses the handle to create its consumer). The stream covers E::SUBJECT
plus any COMPATIBLE_SUBJECTS, using the connection’s event retention
policy.
Sourcepub(crate) async fn ensure_command_stream(
&self,
subject_filter: &str,
) -> Result<Stream, CreateStreamError>
pub(crate) async fn ensure_command_stream( &self, subject_filter: &str, ) -> Result<Stream, CreateStreamError>
Ensures the WorkQueue JetStream stream for a command family exists and
returns its handle.
Mirrors NatsClient::ensure_event_stream. Shared by the responder
(Responder::respond) and the
requester (Requester::request)
so both agree on a single stream per SUBJECT_FILTER. The responder
uses the handle to create its consumer; the requester just needs the
stream to exist before publishing (and discards the handle), so a
command sent before its responder is up is still durably captured.
The retention policy is always WorkQueue; only the limits come from
MessageBusConfig::command_retention.
Source§impl NatsClient
impl NatsClient
Sourceasync fn respond_inner<C: Command + Send + 'static>(
&self,
) -> Result<impl Stream<Item = Result<NatsReceivedCommand<C>, Error>> + Send + 'static + use<C>, Error>
async fn respond_inner<C: Command + Send + 'static>( &self, ) -> Result<impl Stream<Item = Result<NatsReceivedCommand<C>, Error>> + Send + 'static + use<C>, Error>
Helper that Responder::respond delegates to: creates the initial
command stream up front (so setup errors surface synchronously) and
wraps it in a resubscribing_stream that re-creates the stream after
each disconnect.
Sourceasync fn make_command_stream<C: Command + 'static>(
&self,
) -> Result<Pin<Box<dyn Stream<Item = Result<NatsReceivedCommand<C>, Error>> + Send>>, Error>
async fn make_command_stream<C: Command + 'static>( &self, ) -> Result<Pin<Box<dyn Stream<Item = Result<NatsReceivedCommand<C>, Error>> + Send>>, Error>
Creates the durable pull consumer for C on its WorkQueue stream and
returns the commands it delivers, pulling at most
command_prefetch at a
time so idle instances can take the rest.
Source§impl NatsClient
impl NatsClient
Sourceasync fn subscribe_inner<E: Event + 'static>(
&self,
) -> Result<Pin<Box<dyn Stream<Item = Result<NatsReceivedEvent<E>, Error>> + Send>>, Error>
async fn subscribe_inner<E: Event + 'static>( &self, ) -> Result<Pin<Box<dyn Stream<Item = Result<NatsReceivedEvent<E>, Error>> + Send>>, Error>
Core subscription logic: creates the JetStream stream and consumer and returns a pinned stream of deserialized events.
Called by Subscriber::subscribe for the initial subscription and
again by the background reconnect task after each disconnect.
Trait Implementations§
Source§impl Clone for NatsClient
impl Clone for NatsClient
Source§fn clone(&self) -> NatsClient
fn clone(&self) -> NatsClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NatsClient
impl Debug for NatsClient
Source§impl Publisher for NatsClient
impl Publisher for NatsClient
Source§async fn publish<E>(&self, message: E) -> Result<(), Error>
async fn publish<E>(&self, message: E) -> Result<(), Error>
Serializes message to JSON and publishes it to the JetStream subject
defined by Event::SUBJECT. Creates the stream if it does not yet
exist (idempotent), using the retention policy from
MessageBusConfig::retention.
The current OpenTelemetry span is injected into message headers.
Source§impl Requester for NatsClient
impl Requester for NatsClient
Source§async fn request<C: Command + Send>(&self, cmd: C) -> Result<C::Reply, Error>
async fn request<C: Command + Send>(&self, cmd: C) -> Result<C::Reply, Error>
Sends cmd over JetStream and awaits a typed reply on a core-NATS
inbox.
Encodes the command payload with Command::into_payload, ensures the
command’s JetStream stream exists, then publishes to the subject from
Command::subject with the reply subject carried in the header.
Ensuring the stream means a command sent before its responder is up is
durably captured rather than dropped.
Source§impl Responder for NatsClient
impl Responder for NatsClient
Source§fn respond<C: Command + Send + 'static>(
self,
) -> impl Future<Output = Result<impl Stream<Item = Result<impl ReceivedCommand<C>, Error>> + Send + 'static, Error>> + Send + 'static
fn respond<C: Command + Send + 'static>( self, ) -> impl Future<Output = Result<impl Stream<Item = Result<impl ReceivedCommand<C>, Error>> + Send + 'static, Error>> + Send + 'static
Creates a JetStream stream and durable pull consumer for C::SUBJECT,
then returns a stream that yields received commands.
The stream uses WorkQueue retention so each message is delivered to
exactly one instance in the consumer group. Multiple instances sharing
the same service_name automatically load-balance.
Source§impl Subscriber for NatsClient
impl Subscriber for NatsClient
Source§async fn subscribe<E: Event + 'static>(
&self,
) -> Result<impl Stream<Item = Result<impl ReceivedEvent<E> + Send + Sync + 'static, Error>> + Send + 'static, Error>
async fn subscribe<E: Event + 'static>( &self, ) -> Result<impl Stream<Item = Result<impl ReceivedEvent<E> + Send + Sync + 'static, Error>> + Send + 'static, Error>
Subscribes to E::SUBJECT using a durable JetStream pull consumer
named after this connection’s service_name.
Creates the stream and consumer synchronously before returning (so a
publish immediately after subscribe() will not be lost). A background
task then forwards messages through an internal channel and
automatically re-subscribes if the NATS connection drops, using
exponential backoff.
Multiple service instances with the same service_name will
automatically load-balance message delivery.
Auto Trait Implementations§
impl !RefUnwindSafe for NatsClient
impl !UnwindSafe for NatsClient
impl Freeze for NatsClient
impl Send for NatsClient
impl Sync for NatsClient
impl Unpin for NatsClient
impl UnsafeUnpin for NatsClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> JsonCastable<CanonicalJsonValue> for T
impl<T> JsonCastable<Value> for T
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more