pub struct FastPublisher { /* private fields */ }Expand description
A non-atomic, high-throughput JetStream batch publisher using the fast-ingest protocol (ADR-50, nats-server 2.14+).
Obtain via FastPublishExt::fast_publish → FastPublisherBuilder::build.
A FastPublisher is Send but NOT Sync: methods require &mut self and
the publisher must be driven from a single task. Dropping the publisher
mid-batch is safe — the underlying async_nats::Subscriber drops with
it, the server-side interest is torn down, and the server will time out
the abandoned batch after 10 seconds.
Implementations§
Source§impl FastPublisher
impl FastPublisher
Sourcepub fn size(&self) -> u64
pub fn size(&self) -> u64
Returns the number of messages added to (and published in) this batch so far, excluding any pending commit message.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true if the batch has been committed, closed, or failed
fatally.
Sourcepub fn batch_id(&self) -> &str
pub fn batch_id(&self) -> &str
Returns the batch’s inbox, which is also the batch identifier as seen by the server.
Sourcepub fn last_ack_sequence(&self) -> u64
pub fn last_ack_sequence(&self) -> u64
Returns the highest batch sequence acknowledged by the server so far.
0 before the first flow ack arrives.
Source§impl FastPublisher
impl FastPublisher
Sourcepub async fn add<S: ToSubject>(
&mut self,
subject: S,
payload: Bytes,
) -> Result<FastPubAck, FastPublishError>
pub async fn add<S: ToSubject>( &mut self, subject: S, payload: Bytes, ) -> Result<FastPubAck, FastPublishError>
Add a message to the batch with the given subject and payload.
Convenience wrapper around add_message for
callers that don’t need custom headers.
Sourcepub async fn add_message(
&mut self,
msg: OutboundMessage,
) -> Result<FastPubAck, FastPublishError>
pub async fn add_message( &mut self, msg: OutboundMessage, ) -> Result<FastPubAck, FastPublishError>
Add a pre-constructed message to the batch.
The message’s subject, payload, and headers fields are forwarded
to the server; the reply subject is always set by the publisher.
On the first call, a subscription to the batch inbox is created and the publisher waits for the initial flow ack from the server to confirm the batch has been accepted.
§Errors
FastPublishErrorKind::Closedif the publisher has already been committed, closed, or failed fatally.FastPublishErrorKind::Subscribeif the initial subscription fails.FastPublishErrorKind::Publishif publishing the message fails.FastPublishErrorKind::Timeoutif the initial ack does not arrive withinack_timeout.- Any mapped API error from the server’s init response
(
NotEnabled,InvalidPattern,InvalidBatchId,UnknownBatchId,TooManyInflight).
Sourcepub async fn commit<S: ToSubject>(
self,
subject: S,
payload: Bytes,
) -> Result<BatchPubAck, FastPublishError>
pub async fn commit<S: ToSubject>( self, subject: S, payload: Bytes, ) -> Result<BatchPubAck, FastPublishError>
Commit the batch by publishing a final stored message.
After this returns, the publisher is closed and no further messages
can be added. The returned BatchPubAck includes the batch id (the
publisher’s inbox) and the total number of messages in the batch.
Sourcepub async fn commit_message(
self,
msg: OutboundMessage,
) -> Result<BatchPubAck, FastPublishError>
pub async fn commit_message( self, msg: OutboundMessage, ) -> Result<BatchPubAck, FastPublishError>
Commit the batch with a pre-constructed final message.
Sourcepub async fn close(self) -> Result<BatchPubAck, FastPublishError>
pub async fn close(self) -> Result<BatchPubAck, FastPublishError>
End the batch without storing a final message (end-of-batch commit).
Uses the first message’s subject as the publish target; the server
does not persist the commit message itself. Returns the same
BatchPubAck shape as commit.
Returns FastPublishErrorKind::EmptyBatch if no messages have been
added yet.