Skip to main content

OwnedTransactions

Trait OwnedTransactions 

Source
pub trait OwnedTransactions: Publisher {
    type Transaction: Transaction;

    // Required method
    fn transaction(
        &self,
    ) -> impl Future<Output = Result<Self::Transaction, Self::Error>> + Send;
}
Expand description

A publisher that opens caller-owned, client-buffered transactions.

This is the owned kind of the two transaction capabilities, the counterpart of the borrowed TransactionalPublisher:

  • owned (this trait): every transaction call opens its own independent transaction, and the returned Transaction value owns the buffer. Double-begin is unrepresentable - there is no shared “the transaction” to collide on - and concurrent transactions on one handle are legal.
  • borrowed (TransactionalPublisher): the handle carries the broker’s single transaction and a begin claims it exclusively, so a second begin while one is open errors.

Implement it when the broker’s transactions are client buffers flushed at commit (an AMQP confirms buffer, a Redis pipeline, the in-memory broker). Kafka-like brokers, whose client object holds exactly one transaction per producer, implement only the borrowed kind.

§Examples

use ruststream::{OutgoingMessage, OwnedTransactions, Transaction};

async fn dual_write<P: OwnedTransactions>(
    publisher: &P,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut orders = publisher.transaction().await?;
    let mut audit = publisher.transaction().await?; // concurrent with `orders`
    orders.publish(OutgoingMessage::new("orders", b"{}".as_slice())).await?;
    audit.publish(OutgoingMessage::new("audit", b"{}".as_slice())).await?;
    orders.commit().await?;
    audit.commit().await?;
    Ok(())
}

Required Associated Types§

Source

type Transaction: Transaction

The buffer-owning transaction opened by transaction.

Required Methods§

Source

fn transaction( &self, ) -> impl Future<Output = Result<Self::Transaction, Self::Error>> + Send

Opens a new transaction owned by the returned value.

Every call opens its own independent transaction: settling one never affects another, and the handle keeps publishing directly (Publisher::publish) while any number of them are open.

§Errors

Returns Publisher::Error when the broker refuses to open a transaction; pure client-buffer implementations are infallible in practice.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§