pub struct AsyncRawConnection<S> { /* private fields */ }Expand description
An async raw connection to a Hyper server.
This is the async equivalent of RawConnection,
using tokio’s async I/O traits instead of std’s sync I/O.
The connection is generic over the stream type S, allowing it to work
with different transport mechanisms (TcpStream, TlsStream, etc.) as long as they
implement AsyncRead + AsyncWrite + Unpin.
Implementations§
Source§impl<S> AsyncRawConnection<S>
impl<S> AsyncRawConnection<S>
Sourcepub fn new(stream: S) -> Self
pub fn new(stream: S) -> Self
Creates a new async raw connection from a stream.
Initializes read and write buffers with default capacity (64 KB each).
The connection is not yet authenticated - call startup() to begin
the connection handshake.
Sourcepub fn is_healthy(&self) -> bool
pub fn is_healthy(&self) -> bool
Returns true if this connection is still in a known-good state
and safe to use for new requests. See
super::connection::RawConnection::is_healthy for the full
semantics — this is the async mirror with identical behavior.
Sourcepub fn mark_desynchronized(&mut self)
pub fn mark_desynchronized(&mut self)
Marks this connection as desynchronized.
Used by async result streams that are dropped mid-iteration: the
Drop impl cannot await to drain trailing ErrorResponse + ReadyForQuery messages after sending a cancel, so it flags the
connection so the next operation short-circuits with a clear error
rather than hanging or misinterpreting stale server output.
Sourcepub fn process_id(&self) -> i32
pub fn process_id(&self) -> i32
Returns the process ID assigned by the server.
Sourcepub fn secret_key(&self) -> i32
pub fn secret_key(&self) -> i32
Returns the secret key for cancel requests.
Sourcepub fn stream_mut(&mut self) -> &mut S
pub fn stream_mut(&mut self) -> &mut S
Returns a mutable reference to the underlying stream.
Sourcepub fn parameter_status(&self, name: &str) -> Option<&str>
pub fn parameter_status(&self, name: &str) -> Option<&str>
Returns a server parameter value by name.
Sourcepub fn queue_copy_fail(&mut self, reason: &str)
pub fn queue_copy_fail(&mut self, reason: &str)
Queues a CopyFail message in the write buffer (synchronous).
Called from AsyncCopyInWriter::Drop when a COPY session is abandoned
without finish() or cancel(). The CopyFail is written to the buffer
but NOT flushed (we can’t do async I/O from Drop). The next async
operation will call drain_pending_copy_cancel to flush and drain
the server’s ErrorResponse + ReadyForQuery before proceeding.
Sourcepub async fn drain_pending_copy_cancel(&mut self) -> Result<()>
pub async fn drain_pending_copy_cancel(&mut self) -> Result<()>
Drains a pending COPY cancel that was queued by queue_copy_fail().
If pending_copy_cancel is set, this flushes the CopyFail message to
the server and reads messages until ReadyForQuery, restoring the
connection to a usable state. Called automatically at the start of
new operations (simple_query, query_binary, start_copy_in*).
§Errors
Returns Error (I/O) if flushing the queued CopyFail or
reading the server’s drain responses fails. A successful drain
clears pending_copy_cancel.
Sourcepub async fn startup(
&mut self,
params: &[(&str, &str)],
password: Option<&str>,
) -> Result<()>
pub async fn startup( &mut self, params: &[(&str, &str)], password: Option<&str>, ) -> Result<()>
Sends a startup message and performs initial handshake (async).
§Errors
- Returns
Error(auth) when the server requests an auth method and no password is supplied, when the offered SASL mechanisms exclude SCRAM-SHA-256, or when SCRAM state is missing at the SASL-continue / SASL-final step. - Returns
Error(server) when the server sends anErrorResponseduring startup (unknown user, unknown database, etc.). - Returns
Error(protocol) if a message arrives out of sequence. - Returns
Error(I/O) on transport read/write failure.
Sourcepub async fn simple_query(&mut self, query: &str) -> Result<Vec<Message>>
pub async fn simple_query(&mut self, query: &str) -> Result<Vec<Message>>
Sends a simple query and returns all messages until ReadyForQuery (async).
§Errors
- Returns
Error(connection) if the connection has been marked unhealthy. - Returns
Error(server) when the server emits anErrorResponse(SQL error, constraint violation, etc.). - Returns
Error(I/O) /Error(closed) on transport read/write failure. - Propagates any error from
Self::drain_pending_copy_cancelwhen a queuedCopyFailneeds to be flushed first.
Sourcepub async fn query_binary(&mut self, query: &str) -> Result<Vec<Message>>
pub async fn query_binary(&mut self, query: &str) -> Result<Vec<Message>>
Sends a query using extended protocol with binary format results (async).
§Errors
Same failure modes as Self::simple_query.
Sourcepub async fn start_query_binary(&mut self, query: &str) -> Result<()>
pub async fn start_query_binary(&mut self, query: &str) -> Result<()>
Starts a binary query but leaves result consumption to the caller (async).
§Errors
- Returns
Error(connection) if the connection is unhealthy. - Returns
Error(I/O) on transport write failure. - Propagates any error from
Self::drain_pending_copy_cancel.
Sourcepub async fn start_simple_query(&mut self, query: &str) -> Result<()>
pub async fn start_simple_query(&mut self, query: &str) -> Result<()>
Starts a simple query but leaves result consumption to the caller (async).
§Errors
Same failure modes as Self::start_query_binary.
Sourcepub async fn start_execute_prepared(
&mut self,
statement_name: &str,
params: &[Option<&[u8]>],
column_count: usize,
) -> Result<()>
pub async fn start_execute_prepared( &mut self, statement_name: &str, params: &[Option<&[u8]>], column_count: usize, ) -> Result<()>
Starts an execute of a prepared statement but leaves result consumption to the caller (async).
Async mirror of
super::connection::RawConnection::start_execute_prepared. See
that method’s docs for the split format-code rationale (params
use 1 = PG binary/BE, results use 2 = HyperBinary/LE).
§Errors
- Returns
Error(connection) if the connection is unhealthy. - Returns
Error(I/O) on transport write failure. - Propagates any error from
Self::drain_pending_copy_cancel.
Sourcepub async fn read_message(&mut self) -> Result<Message>
pub async fn read_message(&mut self) -> Result<Message>
Reads a single message from the server (async).
§Errors
- Returns
Error(I/O) if reading from the transport fails or ifMessage::parsereports a malformed frame. - Returns
Error(closed) when the transport reaches EOF (server closed the connection).
Sourcepub async fn drain_until_ready(&mut self)
pub async fn drain_until_ready(&mut self)
Async equivalent of
super::connection::RawConnection::drain_until_ready. Unbounded;
prefer drain_until_ready_bounded
in destructors and other code paths where blocking indefinitely is
unacceptable. Drain errors are logged via tracing::warn! and then
swallowed.
Sourcepub async fn drain_until_ready_bounded(&mut self, max_messages: usize) -> bool
pub async fn drain_until_ready_bounded(&mut self, max_messages: usize) -> bool
Async equivalent of
super::connection::RawConnection::drain_until_ready_bounded.
See that function’s docs for the full semantics, including why we do
not send a Sync before draining (it would produce an extra
ReadyForQuery on the wire and corrupt the next query’s response).
Sourcepub async fn consume_error(&mut self, body: &ErrorResponseBody) -> Error
pub async fn consume_error(&mut self, body: &ErrorResponseBody) -> Error
Async equivalent of
super::connection::RawConnection::consume_error. Parse the error
body and drain the rest of the response in one call. Semantics are
identical to the sync version, including the
POST_ERROR_DRAIN_CAP
safety valve — see that function’s docs for the rationale. Unbounded
drain would be particularly dangerous here because a stalled read
on the underlying async stream would hang the caller’s future
indefinitely with no observable symptom; the bounded drain turns
that into a loud tracing::warn! plus a connection marked for
reconnect on next use.
Sourcepub async fn start_copy_in(
&mut self,
table_name: &str,
columns: &[&str],
) -> Result<()>
pub async fn start_copy_in( &mut self, table_name: &str, columns: &[&str], ) -> Result<()>
Initiates a COPY IN operation with HyperBinary format (async).
§Errors
Same failure modes as Self::start_copy_in_with_format.
Sourcepub async fn start_copy_in_with_format(
&mut self,
table_name: &str,
columns: &[&str],
format: &str,
) -> Result<()>
pub async fn start_copy_in_with_format( &mut self, table_name: &str, columns: &[&str], format: &str, ) -> Result<()>
Initiates a COPY IN operation with a specified format (async).
§Errors
- Returns
Error(connection) if the connection has been marked unhealthy. - Returns
Error(server) if the server rejects the generatedCOPY ... FROM STDINstatement. - Returns
Error(I/O) on transport read/write failure. - Propagates any error from
Self::drain_pending_copy_cancel.
Sourcepub fn send_copy_data(&mut self, data: &[u8]) -> Result<()>
pub fn send_copy_data(&mut self, data: &[u8]) -> Result<()>
Sends COPY data to the server (sync - just buffers).
§Errors
Currently infallible — frame construction is pure. The Result
return type is preserved for forward compatibility.
Sourcepub async fn send_copy_data_direct(&mut self, data: &[u8]) -> Result<()>
pub async fn send_copy_data_direct(&mut self, data: &[u8]) -> Result<()>
Sends COPY data directly to the stream without internal buffering (async).
This writes the CopyData message directly to the TCP stream, letting
the kernel’s TCP stack handle buffering. Use flush_stream() periodically
to ensure data is sent.
§Errors
Sourcepub async fn flush_stream(&mut self) -> Result<()>
pub async fn flush_stream(&mut self) -> Result<()>
Sourcepub async fn finish_copy(&mut self) -> Result<u64>
pub async fn finish_copy(&mut self) -> Result<u64>
Sourcepub async fn cancel_copy(&mut self, reason: &str) -> Result<()>
pub async fn cancel_copy(&mut self, reason: &str) -> Result<()>
Sourcepub async fn copy_out(&mut self, query: &str) -> Result<Vec<u8>>
pub async fn copy_out(&mut self, query: &str) -> Result<Vec<u8>>
Executes a COPY … TO STDOUT query and returns all output data (async).
§Errors
Sourcepub async fn prepare(
&mut self,
name: &str,
query: &str,
param_types: &[Oid],
) -> Result<(Vec<Oid>, Vec<Column>)>
pub async fn prepare( &mut self, name: &str, query: &str, param_types: &[Oid], ) -> Result<(Vec<Oid>, Vec<Column>)>
Sourcepub async fn execute_prepared(
&mut self,
statement_name: &str,
params: &[Option<&[u8]>],
column_count: usize,
) -> Result<Vec<Row>>
pub async fn execute_prepared( &mut self, statement_name: &str, params: &[Option<&[u8]>], column_count: usize, ) -> Result<Vec<Row>>
Executes a prepared statement with parameters (async).
§Errors
- Returns
Error(connection) if the connection is unhealthy. - Returns
Error(server) ifBind/Executefails on the server (parameter type mismatch, constraint violation, etc.). - Returns
Error(I/O) /Error(closed) on transport read/write failure. - Propagates row-construction errors from
super::row::Row::newif aDataRowcannot be decoded against the reportedRowDescription.
Sourcepub async fn execute_prepared_no_result(
&mut self,
statement_name: &str,
params: &[Option<&[u8]>],
) -> Result<u64>
pub async fn execute_prepared_no_result( &mut self, statement_name: &str, params: &[Option<&[u8]>], ) -> Result<u64>
Executes a prepared statement that doesn’t return rows (async).
§Errors
Same failure modes as Self::execute_prepared (excluding
row-construction errors — this path never builds rows).
Sourcepub async fn close_statement(&mut self, statement_name: &str) -> Result<()>
pub async fn close_statement(&mut self, statement_name: &str) -> Result<()>
Closes a prepared statement (async).
§Errors
- Returns
Error(connection) if the connection is unhealthy. - Returns
Error(server) if the server reports anErrorResponseduringClose/Sync. - Returns
Error(I/O) /Error(closed) on transport read/write failure. - Propagates any error from
Self::drain_pending_copy_cancel.
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for AsyncRawConnection<S>where
S: Freeze,
impl<S> RefUnwindSafe for AsyncRawConnection<S>where
S: RefUnwindSafe,
impl<S> Send for AsyncRawConnection<S>where
S: Send,
impl<S> Sync for AsyncRawConnection<S>where
S: Sync,
impl<S> Unpin for AsyncRawConnection<S>where
S: Unpin,
impl<S> UnsafeUnpin for AsyncRawConnection<S>where
S: UnsafeUnpin,
impl<S> UnwindSafe for AsyncRawConnection<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
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request