pub struct ConfirmSmoother { /* fields omitted */ }
Expand description

Helper to smooth out of order and/or multiple: true publisher confirmation messages.

If publisher confirms are enabled, the server may confirm messages out of order and/or may confirm multiple messages with a single Confirm. ConfirmSmoother exists to “smooth” server messages out into an always-increasing-by-one sequence of confirmation messages.

Example

use amiquip::{Confirm, ConfirmSmoother};
use crossbeam_channel::Receiver;

// assume we've published n messages and want to wait for them to be confirmed
fn wait_for_publisher_confirms(n: usize, receiver: &Receiver<Confirm>) {
    // NOTE: a new smoother assumes we will be receiving messages starting with
    // delivery_tag = 1, so this method is only valid if called after the first n
    // publishes on a channel. We could take a &mut ConfirmSmoother to be called
    // multiple times in succession on the same channel.
    let mut smoother = ConfirmSmoother::new();
    let mut acked = 0;
    let mut nacked = 0;
    while acked + nacked < n {
        // get a confirmation from the server; this may be out of order or a confirm
        // for multiple messages in one payload
        let raw_confirm = match receiver.recv() {
            Ok(raw_confirm) => raw_confirm,
            Err(_) => {
                // the I/O thread dropped the sending side; either an error has occurred
                // or another thread of ours closed the connection; either way we'll
                // stop waiting
                return;
            }
        };

        // feed the raw confirm into the smoother. Two notes:
        //   1. We must run the returned iterator to the end or risk missing confirms.
        //   2. The iterator may produce 0, 1, or multiple independent confirmations.
        //      They will all have multiple: false.
        for confirm in smoother.process(raw_confirm) {
            match confirm {
                Confirm::Ack(_) => {
                    acked += 1;
                }
                Confirm::Nack(_) => {
                    // server rejected message; need to do something else to
                    // track which messages were rejected
                    nacked += 1;
                }
            }
        }
    }
}

Implementations

Create a new ConfirmSmoother. It expects the next (in absolute order) delivery tag received from the server to be 1.

Create a new ConfirmSmoother. It expects the next (in absolute order) delivery tag received from the server to be expected.

Process a confirmation message from the server. Returns an iterator; each item returned by the iterator will be a single (i.e., multiple: false) Confirm. You must run the iterator to its completion or risk missing confirmations; future calls to process will not return an iterator that will repeat confirms that would have been returned by a previously returned iterator (even if that earlier iterator was dropped before it ran to completion).

The returned iterator may have 0 items (if confirm is a non-multiple confirmation that is later than the next expected delivery tag), 1 item (if confirm exactly matches our next expected delivery tag and we had not previously seen the next tag), or multiple items (if confirm is a multiple: true confirmation or we’ve previously seen out-of-order tags that are next sequentially after confirm’s tag).

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.