pub struct TransactionScope<'a, P, C> { /* private fields */ }Expand description
A live broker transaction, opened by Transactional::begin.
Publishes issued through the scope become visible together on commit, or
not at all after abort; both consume the scope, so a double commit or a
publish after settling is a compile error. This is the manual counterpart of the per-batch
transaction the runtime drives for include_batch_publishing mounts.
The scope encodes values with the wrapper’s codec and sends them directly: the reply
PublishTransform stack and the app-wide publish_layer
middleware do not run here - both belong to the dispatch path, where a delivery context
exists.
Dropping an unsettled scope logs a warning and leaves the broker transaction open (destructors cannot run async work); always settle explicitly.
Implementations§
Source§impl<P, C> TransactionScope<'_, P, C>where
P: TransactionalPublisher,
C: Codec,
impl<P, C> TransactionScope<'_, P, C>where
P: TransactionalPublisher,
C: Codec,
Sourcepub async fn publish<T: Serialize + Sync>(
&mut self,
name: &str,
value: &T,
) -> Result<(), TransactionPublishError<P::Error>>
pub async fn publish<T: Serialize + Sync>( &mut self, name: &str, value: &T, ) -> Result<(), TransactionPublishError<P::Error>>
Encodes value with the wrapper’s codec and publishes it to name inside the
transaction.
A failed publish does not settle the scope: the caller decides between retrying and
abort. Aborting is the safe default - after an error the broker-side
transaction state is implementation-defined.
§Errors
Returns TransactionPublishError::Encode when the codec rejects the value, and
TransactionPublishError::Publish when the broker rejects the message.
§Cancel safety
As with Publisher::publish, cancel safety is broker-defined: dropping the future
mid-flight may leave the message in an indeterminate state inside the transaction.
Sourcepub async fn commit(self) -> Result<(), P::Error>
pub async fn commit(self) -> Result<(), P::Error>
Commits the transaction: every publish issued through the scope becomes visible at once.
§Errors
Returns the publisher’s error when the broker rejects the commit. Per the
TransactionalPublisher contract a failed commit closes the transaction, so the spent
scope leaves the handle free for a fresh begin.
§Cancel safety
Not cancel-safe: dropping the future mid-commit leaves the broker transaction in an
implementation-defined state. The scope treats itself as unsettled then - its drop
warning fires - which is why open clears only after the broker call completes.