pub struct PubSocket { /* private fields */ }Expand description
A PUB socket for broadcasting messages to multiple subscribers.
PubSocket uses a worker pool architecture to handle multiple subscribers efficiently:
- Multiple OS threads (default: CPU core count)
- Each worker runs its own compio runtime with io_uring
- Round-robin subscriber distribution across workers
- Zero-copy message broadcasting via
Arc<Bytes> - Lock-free subscription management
§Example
use monocoque::zmq::PubSocket;
use bytes::Bytes;
let mut socket = PubSocket::bind("127.0.0.1:5555").await?;
// Accept subscribers (non-blocking with worker pool)
socket.accept_subscriber().await?;
// Broadcast to all subscribers
socket.send(vec![Bytes::from("topic"), Bytes::from("data")]).await?;Implementations§
Source§impl PubSocket
impl PubSocket
Sourcepub async fn bind(addr: impl ToSocketAddrs) -> Result<Self>
pub async fn bind(addr: impl ToSocketAddrs) -> Result<Self>
Bind to an address with default worker count (CPU cores).
Sourcepub async fn bind_with_workers(
addr: impl ToSocketAddrs,
worker_count: usize,
) -> Result<Self>
pub async fn bind_with_workers( addr: impl ToSocketAddrs, worker_count: usize, ) -> Result<Self>
Bind with a specific number of worker threads.
Sourcepub async fn accept_subscriber(&mut self) -> Result<u64>
pub async fn accept_subscriber(&mut self) -> Result<u64>
Accept a new subscriber connection.
Performs ZMTP handshake and assigns the subscriber to a worker thread. Returns the subscriber ID.
Sourcepub async fn send(&mut self, msg: Vec<Bytes>) -> Result<()>
pub async fn send(&mut self, msg: Vec<Bytes>) -> Result<()>
Broadcast a multipart message to all matching subscribers.
Messages are distributed to all workers in parallel. The first frame is typically used as a topic for subscription filtering.
Sourcepub async fn send_frames(&mut self, frames: &[Bytes]) -> Result<()>
pub async fn send_frames(&mut self, frames: &[Bytes]) -> Result<()>
Broadcast a message given as borrowed frames.
Allocation-light counterpart to send: the shared message
is allocated only when it matches a subscription, so publishing from a
stack array (send_frames(&[topic, payload])) pays no per-message heap
allocation on the common drop path of a topic-filtered stream.
Sourcepub const fn subscriber_count(&self) -> usize
pub const fn subscriber_count(&self) -> usize
Get the number of active subscribers.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Get the local address this socket is bound to.
Sourcepub const fn socket_type() -> SocketType
pub const fn socket_type() -> SocketType
Sourcepub fn monitor(&mut self) -> SocketMonitor
pub fn monitor(&mut self) -> SocketMonitor
Enable monitoring for this socket.
Returns a receiver for socket lifecycle events.
Sourcepub fn options_mut(&mut self) -> &mut SocketOptions
pub fn options_mut(&mut self) -> &mut SocketOptions
Get a mutable reference to this socket’s options.
Sourcepub fn drop_count(&self) -> u64
pub fn drop_count(&self) -> u64
Number of messages dropped due to HWM backpressure.
Auto Trait Implementations§
impl !Freeze for PubSocket
impl !RefUnwindSafe for PubSocket
impl !Send for PubSocket
impl !Sync for PubSocket
impl !UnwindSafe for PubSocket
impl Unpin for PubSocket
impl UnsafeUnpin for PubSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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