pub struct PushSocket<S = TcpStream>{ /* private fields */ }Expand description
PUSH socket for distributing messages in a pipeline.
PUSH sockets send messages to connected PULL sockets in a round-robin fashion, providing load balancing for parallel processing.
Implementations§
Source§impl<S> PushSocket<S>
impl<S> PushSocket<S>
Sourcepub async fn new(stream: S) -> Result<Self>
pub async fn new(stream: S) -> Result<Self>
Create a new PUSH socket from a stream with default buffer configuration.
Sourcepub async fn with_options(stream: S, options: SocketOptions) -> Result<Self>
pub async fn with_options(stream: S, options: SocketOptions) -> Result<Self>
Create a new PUSH socket with custom buffer configuration and socket options.
Sourcepub async fn send(&mut self, msg: Vec<Bytes>) -> Result<()>
pub async fn send(&mut self, msg: Vec<Bytes>) -> Result<()>
Send a message to a connected PULL socket.
Messages are distributed in a round-robin fashion when multiple PULL sockets are connected (in a multi-connection scenario).
By default each call writes to the kernel immediately (eager mode, one
io_uring operation per message). For throughput-bound pipelines, enable write
coalescing via SocketOptions::with_write_coalescing and call
flush after the last send in each burst. In coalesced mode,
bytes may remain in userspace until the 64 KB threshold fills or flush() is
called explicitly.
§Errors
Returns an error if the socket is poisoned, disconnected, or if the write fails.
Sourcepub async fn send_one(&mut self, frame: Bytes) -> Result<()>
pub async fn send_one(&mut self, frame: Bytes) -> Result<()>
Send a single-frame message without allocating a one-element Vec.
This is equivalent to send(vec![frame]), but keeps the hot path for
single-frame PUSH/PULL pipelines from measuring the caller’s multipart
container allocation.
Sourcepub async fn flush(&mut self) -> Result<()>
pub async fn flush(&mut self) -> Result<()>
Flush any messages still buffered by write coalescing.
Call this after the last send() in a burst when write_coalescing is
enabled to ensure all pending data is written to the kernel.
Sourcepub async fn send_batch<I>(&mut self, msgs: I) -> Result<usize>
pub async fn send_batch<I>(&mut self, msgs: I) -> Result<usize>
Encode and send a batch of messages in a single kernel write.
Encodes every message in msgs into the send buffer, then flushes once.
This gives the same kernel-call efficiency as write coalescing but with
explicit batch boundaries - no threshold check and no flush() required.
Works independently of the write_coalescing option and can be mixed
with send() calls freely.
Returns the number of messages sent.
Sourcepub async fn close(self) -> Result<()>
pub async fn close(self) -> Result<()>
Close the socket gracefully by shutting down the underlying stream.
Sourcepub const fn options(&self) -> &SocketOptions
pub const fn options(&self) -> &SocketOptions
Get a reference to the socket options.
Sourcepub fn options_mut(&mut self) -> &mut SocketOptions
pub fn options_mut(&mut self) -> &mut SocketOptions
Get a mutable reference to the socket options.
Sourcepub fn set_options(&mut self, options: SocketOptions)
pub fn set_options(&mut self, options: SocketOptions)
Set socket options (builder-style).
Source§impl PushSocket<TcpStream>
impl PushSocket<TcpStream>
Sourcepub async fn from_tcp(stream: TcpStream) -> Result<Self>
pub async fn from_tcp(stream: TcpStream) -> Result<Self>
Create a new PUSH socket from a TCP stream with TCP_NODELAY enabled.
Sourcepub async fn from_tcp_with_options(
stream: TcpStream,
options: SocketOptions,
) -> Result<Self>
pub async fn from_tcp_with_options( stream: TcpStream, options: SocketOptions, ) -> Result<Self>
Create a new PUSH socket from a TCP stream with TCP_NODELAY and custom options.
Sourcepub async fn connect(addr: impl ToSocketAddrs) -> Result<Self>
pub async fn connect(addr: impl ToSocketAddrs) -> Result<Self>
Connect to a remote PUSH socket, storing the endpoint for automatic reconnection.
Sourcepub async fn connect_with_options(
addr: impl ToSocketAddrs,
options: SocketOptions,
) -> Result<Self>
pub async fn connect_with_options( addr: impl ToSocketAddrs, options: SocketOptions, ) -> Result<Self>
Connect with custom options, storing the endpoint for reconnection.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the socket is currently connected.
Sourcepub async fn try_reconnect(&mut self) -> Result<()>
pub async fn try_reconnect(&mut self) -> Result<()>
Try to reconnect to the stored endpoint.
Sourcepub async fn send_with_reconnect(&mut self, msg: Vec<Bytes>) -> Result<()>
pub async fn send_with_reconnect(&mut self, msg: Vec<Bytes>) -> Result<()>
Send a message with automatic reconnection on network error.
On BrokenPipe / ConnectionReset, write_from_buf() already sets
stream = None, so the next loop iteration reconnects automatically.
Respects max_reconnect_attempts - returns NotConnected when exhausted.
Trait Implementations§
Source§impl ProxySocket for PushSocket
impl ProxySocket for PushSocket
Source§async fn recv_multipart(&mut self) -> Result<Option<Vec<Bytes>>>
async fn recv_multipart(&mut self) -> Result<Option<Vec<Bytes>>>
Source§async fn send_multipart(&mut self, msg: Vec<Bytes>) -> Result<()>
async fn send_multipart(&mut self, msg: Vec<Bytes>) -> Result<()>
Source§fn socket_desc(&self) -> &'static str
fn socket_desc(&self) -> &'static str
Source§impl<S> Socket for PushSocket<S>
impl<S> Socket for PushSocket<S>
Auto Trait Implementations§
impl<S = TcpStream> !Freeze for PushSocket<S>
impl<S> RefUnwindSafe for PushSocket<S>where
S: RefUnwindSafe,
impl<S> Send for PushSocket<S>where
S: Send,
impl<S> Sync for PushSocket<S>where
S: Sync,
impl<S> Unpin for PushSocket<S>
impl<S> UnsafeUnpin for PushSocket<S>where
S: UnsafeUnpin,
impl<S> UnwindSafe for PushSocket<S>where
S: UnwindSafe,
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