Skip to main content

ConnCtx

Struct ConnCtx 

Source
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

Source

pub fn index(&self) -> usize

Returns the connection slot index. Useful for indexing into per-connection arrays.

Source

pub fn token(&self) -> ConnToken

Returns the ConnToken for this connection.

Source

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)n bytes 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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn cancel(&self) -> Result<()>

Cancel pending I/O operations on this connection.

Source

pub fn request_shutdown(&self)

Request graceful shutdown of the worker event loop.

Source

pub fn tls_info(&self) -> Option<TlsInfo>

Query TLS session info for this connection.

Source

pub fn connect_tls( &self, addr: SocketAddr, server_name: &str, ) -> Result<ConnectFuture>

Initiate an outbound TLS connection and await the result.

Source

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.

Source

pub fn close(&self)

Close this connection.

Source

pub fn peer_addr(&self) -> Option<PeerAddr>

Access peer address.

Source

pub fn is_outbound(&self) -> bool

Check if this connection is outbound (initiated via connect).

Source§

impl ConnCtx

Source

pub fn send_parts(&self) -> AsyncSendBuilder

Begin building a scatter-gather send.

On the mio backend, this degrades to copy-only sends.

Trait Implementations§

Source§

impl Clone for ConnCtx

Source§

fn clone(&self) -> ConnCtx

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for ConnCtx

Auto Trait Implementations§

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<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.