pub struct MessageReader { /* private fields */ }Expand description
Cancellation-safe backend message reader.
PostgreSQL backend messages span multiple read operations (5-byte header,
then a variable payload). A naive implementation using read_exact is
not cancellation-safe: if the future is dropped between reads (e.g. by
tokio::select! or tokio::time::timeout), bytes already pulled from the
underlying stream are lost and the next read mis-parses the wire stream.
MessageReader externalizes the partial-read state so it survives across
dropped futures. Each call to read uses one-shot
AsyncReadExt::read (which is cancel-safe) and accumulates progress
on self. If the returned future is dropped, no bytes are lost; the next
invocation resumes from where the previous one left off.
Implementations§
Source§impl MessageReader
impl MessageReader
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn with_max_message_size(self, max: usize) -> Self
pub fn with_max_message_size(self, max: usize) -> Self
Refuse any message whose payload is larger than max bytes.
max is compared against the message’s length field minus 4, so it
excludes the tag and the length field itself. The payload buffer is
sized from the length the server announces, so this bounds the memory
one message can take. It can only lower the limit: a value above
MAX_MESSAGE_SIZE, the default, is capped to it.
A refused message is terminal. Its payload is left on the wire, so the
reader cannot resynchronize and every later read
returns the same PgWireError::MessageTooLarge. Discard the reader
and the connection.