Skip to main content

ZmqQueue

Struct ZmqQueue 

Source
pub struct ZmqQueue<Role = Bind> { /* private fields */ }
Expand description

The PUSH/PULL queue: each message reaches one of the competing consumers.

Role is the side of the endpoint this process takes, fixed by the endpoint’s constructor: Bind for ZmqEndpoint::bind, Connect for ZmqEndpoint::connect. It decides where a retry copy goes. A subscription that binds takes a copy published to its own listener, so a mount site names no destination; one that dials reads from a PUSH peer that takes nothing, so a mount site names where its copies go.

§Examples

A worker that binds its queue retries on it. The copy goes back into the queue, whichever worker is free takes it, and the framework’s retry-count header caps the deliveries at five:

use std::time::Duration;

use ruststream_zeromq::queue::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Job {
    id: u64,
}

#[subscriber("jobs")]
async fn handle(job: &Job) -> HandlerOutcome {
    if job.id == 0 {
        return HandlerOutcome::retry_after(Duration::from_secs(30));
    }
    HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
    RustStream::new(AppInfo::new("worker", "0.1.0")).with_broker(
        ZmqQueue::new(ZmqEndpoint::bind("tcp://0.0.0.0:5555")),
        |b| {
            b.include(handle)
                .max_attempts(nonzero!(5u32))
                .out_retry(Publish);
        },
    )
}

One of several workers that dial the same ventilator and compete for its jobs. The ventilator takes nothing back, so the retry copies go to a queue the worker binds itself, where the same handler takes them. The copies carry the framework’s counter, so the cap holds across both subscriptions:

use std::time::Duration;

use ruststream_zeromq::queue::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Job {
    id: u64,
}

#[subscriber("jobs")]
async fn work(job: &Job) -> HandlerOutcome {
    if job.id == 0 {
        return HandlerOutcome::retry_after(Duration::from_secs(30));
    }
    HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
    let retries = ZmqQueue::new(ZmqEndpoint::bind("tcp://0.0.0.0:5560")).bindable();
    let copies = retries.bind(Publish);
    RustStream::new(AppInfo::new("worker", "0.1.0"))
        .with_broker(ZmqQueue::new(ZmqEndpoint::connect("tcp://ventilator:5555")), |b| {
            b.include(work)
                .max_attempts(nonzero!(5u32))
                .out_retry(copies)
                .to("jobs");
        })
        .with_broker(retries, |b| {
            b.include(work).max_attempts(nonzero!(5u32));
        })
}

Implementations§

Source§

impl<Role: EndpointRole> ZmqQueue<Role>

Source

pub fn new(endpoint: ZmqEndpoint<Role>) -> Self

Records the endpoint. No I/O.

§Examples

A worker that listens for the jobs its producers push:

use ruststream_zeromq::queue::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Job {
    id: u64,
}

#[subscriber("jobs")]
async fn work(job: &Job) -> HandlerOutcome {
    tracing::info!(job.id, "working on the job");
    HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
    RustStream::new(AppInfo::new("worker", "0.1.0")).with_broker(
        ZmqQueue::new(ZmqEndpoint::bind("tcp://0.0.0.0:5555")),
        |b| {
            b.include(work);
        },
    )
}
Source§

impl<Role> ZmqQueue<Role>

Source

pub const fn read_ahead(self, read_ahead: NonZeroUsize) -> Self

How many deliveries a subscription reads off its socket ahead of the handler; 1000 unless set, the receive high-water mark ZeroMQ itself gives a socket.

Past this bound the subscription stops reading, the socket’s buffers fill, and the sender waits: a handler slower than the wire slows the sender down rather than growing this process’s memory.

§Examples

A worker whose renders take seconds holds 64 jobs ahead, and the producers wait beyond that:

use ruststream_zeromq::queue::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Render {
    scene: String,
}

#[subscriber("renders")]
async fn render(job: &Render) -> HandlerOutcome {
    tracing::info!(scene = %job.scene, "rendering");
    HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
    let renders = ZmqQueue::new(ZmqEndpoint::bind("tcp://0.0.0.0:5555"))
        .read_ahead(nonzero!(64_usize));
    RustStream::new(AppInfo::new("renderer", "0.1.0")).with_broker(renders, |b| {
        b.include(render);
    })
}
Source

pub fn publisher(&self) -> ZmqQueuePublisher

A publisher sharing this queue’s state; buildable before connect.

Trait Implementations§

Source§

impl<Role: EndpointRole> Broker for ZmqQueue<Role>

Source§

type Error = ZmqError

The error type returned by broker-level operations.
Source§

type Connected = ConnectedZmqQueue<Role>

The connected form of this broker: the typed witness that connect succeeded.
Source§

async fn connect(self) -> Result<Self::Connected, Self::Error>

Establishes the connection to the broker, consuming the unconnected form. Read more
Source§

fn bindable(self) -> Bindable<Self>
where Self: 'static,

Wraps the broker for publisher-token minting before registration: the returned Bindable hands out Bound tokens via its bind, and is itself what with_broker takes. Read more
Source§

impl<Role> Clone for ZmqQueue<Role>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Role> Debug for ZmqQueue<Role>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Role: EndpointRole> DescribeServer for ZmqQueue<Role>

Source§

fn describe_server(&self) -> ServerSpec

Returns the server coordinates for this broker.
Source§

impl<Role: EndpointRole> InProcess for ZmqQueue<Role>

Source§

async fn connect_in_process(self) -> Result<Self::Connected, Self::Error>

Connects to the in-process transport, consuming the configuration. Read more

Auto Trait Implementations§

§

impl<Role = Bind> !RefUnwindSafe for ZmqQueue<Role>

§

impl<Role = Bind> !UnwindSafe for ZmqQueue<Role>

§

impl<Role> Freeze for ZmqQueue<Role>
where PhantomData<fn() -> Role>: Freeze,

§

impl<Role> Send for ZmqQueue<Role>
where PhantomData<fn() -> Role>: Send,

§

impl<Role> Sync for ZmqQueue<Role>
where PhantomData<fn() -> Role>: Sync,

§

impl<Role> Unpin for ZmqQueue<Role>
where PhantomData<fn() -> Role>: Unpin,

§

impl<Role> UnsafeUnpin for ZmqQueue<Role>
where PhantomData<fn() -> Role>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<B> BrokerRegistration for B
where B: Broker + 'static,

Source§

type Broker = B

The broker being registered.
Source§

fn into_parts(self) -> (B, Arc<Mutex<Option<Arc<<B as Broker>::Connected>>>>)

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PublishedThrough<DefaultSlot> for T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more