pub struct ConnCtx { /* private fields */ }Expand description
Async connection context with send/recv futures.
The async equivalent of ConnToken + DriverCtx. Passed to the
connection’s async fn, provides I/O methods.
Async connection context providing send, recv, and connect operations.
Each accepted connection receives a ConnCtx in AsyncEventHandler::on_accept.
It exposes an async API for reading data (with_data,
with_bytes), sending data (send,
send_nowait), and initiating outbound connections
(connect).
A ConnCtx is valid for the lifetime of the connection’s async task.
When the connection is closed, the task is dropped along with the ConnCtx.
Implementations§
Source§impl ConnCtx
impl ConnCtx
Sourcepub fn index(&self) -> usize
pub fn index(&self) -> usize
Returns the connection slot index. Useful for indexing into per-connection arrays.
Sourcepub fn with_data<F: FnMut(&[u8]) -> ParseResult>(
&self,
f: F,
) -> WithDataFuture<F> ⓘ
pub fn with_data<F: FnMut(&[u8]) -> ParseResult>( &self, f: F, ) -> WithDataFuture<F> ⓘ
Wait until recv data is available, then process it.
The closure receives accumulated bytes and returns a ParseResult:
ParseResult::Consumed(n)—nbytes were consumed from the buffer.ParseResult::NeedMore— the closure needs more data before making progress.
Resolves immediately when data is already buffered (cache-hit hot path).
If the closure returns NeedMore or Consumed(0) on non-empty data
(incomplete parse), the future parks and retries when more data arrives.
The closure must therefore be safe to call multiple times (FnMut).
Sourcepub fn with_bytes<F: FnMut(Bytes) -> ParseResult>(
&self,
f: F,
) -> WithBytesFuture<F> ⓘ
pub fn with_bytes<F: FnMut(Bytes) -> ParseResult>( &self, f: F, ) -> WithBytesFuture<F> ⓘ
Wait until recv data is available, then provide it as zero-copy Bytes.
Like with_data(), but the closure receives a Bytes
handle that can be sliced (O(1), refcounted) instead of copied. The
closure returns a ParseResult indicating bytes consumed.
This enables zero-copy RESP parsing: the parser can call bytes.slice()
to extract sub-ranges without allocating.
Sourcepub unsafe fn set_recv_sink(&self, target: *mut u8, len: usize)
pub unsafe fn set_recv_sink(&self, target: *mut u8, len: usize)
Install a recv sink so that CQE data is written directly to the target buffer instead of the per-connection accumulator.
§Safety
The caller must ensure that target points to writable memory of at
least len bytes, and that the memory remains valid until
take_recv_sink() is called. In practice this
is guaranteed because ringline is single-threaded: the task sets the sink,
yields, and the CQE handler (same thread) writes to it before the task
resumes and clears the sink.
Sourcepub fn take_recv_sink(&self) -> usize
pub fn take_recv_sink(&self) -> usize
Remove the recv sink and return the number of bytes written to it. Returns 0 if no sink was active.
Sourcepub fn recv_ready(&self) -> RecvReadyFuture ⓘ
pub fn recv_ready(&self) -> RecvReadyFuture ⓘ
Returns a future that becomes ready when any recv data is available (in the accumulator, recv sink, or connection is closed).
Use this with set_recv_sink() to wait for
direct-to-buffer writes without processing accumulator data.
Sourcepub fn try_with_data<F: FnOnce(&[u8]) -> ParseResult>(
&self,
f: F,
) -> Option<ParseResult>
pub fn try_with_data<F: FnOnce(&[u8]) -> ParseResult>( &self, f: F, ) -> Option<ParseResult>
Non-blocking accumulator access. Calls f with buffered data if any,
returning Some(result). Returns None if the accumulator is empty.
Sourcepub fn send_nowait(&self, data: &[u8]) -> Result<()>
pub fn send_nowait(&self, data: &[u8]) -> Result<()>
Fire-and-forget send: copies data into the send pool and submits the SQE. One copy, no heap allocation, no future.
This is the hot-path send for cache responses. The CQE is handled internally by the executor (resource cleanup + send queue advancement).
§Errors
Returns Err if the send copy pool is exhausted or the submission queue is full.
For backpressure-aware sending, use send() instead.
Sourcepub fn forward_recv_buf(&self, data: &[u8]) -> Result<()>
pub fn forward_recv_buf(&self, data: &[u8]) -> Result<()>
Forward the current pending recv buffer as a zero-copy send.
This is intended for use inside a with_data closure. If the connection
has a pending recv buffer (from the zero-copy recv path), it is taken and
used as the send source directly — no copy into the send pool. The recv
buffer is replenished when the send completes.
Falls back to send_nowait(data) if there is no pending recv buffer
(e.g., data came from the accumulator or a TLS connection).
Only works for plaintext connections; TLS connections always copy. On the mio backend, this always uses the copy path.
Sourcepub fn send(&self, data: &[u8]) -> Result<SendFuture>
pub fn send(&self, data: &[u8]) -> Result<SendFuture>
Send data and await completion. Copies data into the send pool, submits the SQE eagerly, then returns a future that resolves with the total bytes sent (or error).
Use this when you need backpressure or send completion notification.
For fire-and-forget sending, use send_nowait().
§Errors
Returns Err if the send copy pool is exhausted or the submission queue is full.
Sourcepub fn connect(&self, addr: SocketAddr) -> Result<ConnectFuture>
pub fn connect(&self, addr: SocketAddr) -> Result<ConnectFuture>
Initiate an outbound TCP connection and await the result.
Returns a new ConnCtx for the peer connection on success.
Sourcepub fn connect_with_timeout(
&self,
addr: SocketAddr,
timeout_ms: u64,
) -> Result<ConnectFuture>
pub fn connect_with_timeout( &self, addr: SocketAddr, timeout_ms: u64, ) -> Result<ConnectFuture>
Initiate an outbound TCP connection with a timeout and await the result.
Sourcepub fn connect_unix(&self, path: impl AsRef<Path>) -> Result<ConnectFuture>
pub fn connect_unix(&self, path: impl AsRef<Path>) -> Result<ConnectFuture>
Initiate an outbound Unix domain socket connection and await the result.
Returns a new ConnCtx for the peer connection on success.
Sourcepub fn shutdown_write(&self)
pub fn shutdown_write(&self)
Shutdown the write side of the connection (half-close).
Sends a TCP FIN to the peer. The read side remains open.
Sourcepub fn request_shutdown(&self)
pub fn request_shutdown(&self)
Request graceful shutdown of the worker event loop.
Sourcepub fn connect_tls(
&self,
addr: SocketAddr,
server_name: &str,
) -> Result<ConnectFuture>
pub fn connect_tls( &self, addr: SocketAddr, server_name: &str, ) -> Result<ConnectFuture>
Initiate an outbound TLS connection and await the result.
Sourcepub fn connect_tls_with_timeout(
&self,
addr: SocketAddr,
server_name: &str,
timeout_ms: u64,
) -> Result<ConnectFuture>
pub fn connect_tls_with_timeout( &self, addr: SocketAddr, server_name: &str, timeout_ms: u64, ) -> Result<ConnectFuture>
Initiate an outbound TLS connection with a timeout and await the result.
Sourcepub fn is_outbound(&self) -> bool
pub fn is_outbound(&self) -> bool
Check if this connection is outbound (initiated via connect).
Source§impl ConnCtx
impl ConnCtx
Sourcepub fn send_parts(&self) -> AsyncSendBuilder
pub fn send_parts(&self) -> AsyncSendBuilder
Begin building a scatter-gather send.
On the mio backend, this degrades to copy-only sends.