Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sqlx-core/src/any/connection/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static {
/// [`Connection::shrink_buffers()`]: method@crate::connection::Connection::shrink_buffers
fn shrink_buffers(&mut self);

/// Forward to [`Connection::buffer_stats()`].
///
/// [`Connection::buffer_stats()`]: method@crate::connection::Connection::buffer_stats
fn buffer_stats(&self) -> Option<crate::net::BufferStats> {
None
}

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, crate::Result<()>>;

Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/src/any/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ impl Connection for AnyConnection {
self.backend.shrink_buffers()
}

fn buffer_stats(&self) -> Option<crate::net::BufferStats> {
self.backend.buffer_stats()
}

#[doc(hidden)]
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
self.backend.flush()
Expand Down
11 changes: 11 additions & 0 deletions sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ pub trait Connection: Send {
/// allow the buffers to shrink.
fn shrink_buffers(&mut self);

/// Returns statistics about the connection's internal buffer allocation.
///
/// This can be used to monitor memory usage per connection. The default buffer
/// capacity is 8KB for both read and write buffers, but they may grow to
/// accommodate large queries or result sets.
///
/// Returns `None` for databases that don't use buffered sockets (like SQLite).
fn buffer_stats(&self) -> Option<crate::net::BufferStats> {
None
}

#[doc(hidden)]
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;

Expand Down
3 changes: 2 additions & 1 deletion sqlx-core/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod socket;
pub mod tls;

pub use socket::{
connect_tcp, connect_uds, BufferedSocket, Socket, SocketIntoBox, WithSocket, WriteBuffer,
connect_tcp, connect_uds, BufferStats, BufferedSocket, Socket, SocketIntoBox, WithSocket,
WriteBuffer,
};
37 changes: 37 additions & 0 deletions sqlx-core/src/net/socket/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,41 @@ impl ReadBuffer {
self.available = BytesMut::with_capacity(DEFAULT_BUF_SIZE);
}
}

/// Returns the current allocated capacity of this buffer in bytes.
pub fn capacity(&self) -> usize {
self.read.capacity() + self.available.capacity()
}
}

/// Statistics about connection buffer allocation.
///
/// This can be used to monitor memory usage per connection for observability purposes.
/// The default buffer capacity is 8KB for both read and write buffers, but they may grow
/// to accommodate large queries or result sets.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BufferStats {
/// Allocated capacity of the write buffer in bytes.
pub write_buffer_capacity: usize,
/// Allocated capacity of the read buffer in bytes.
pub read_buffer_capacity: usize,
}

impl WriteBuffer {
/// Returns the current allocated capacity of this buffer in bytes.
pub fn capacity(&self) -> usize {
self.buf.capacity()
}
}

impl<S: Socket> BufferedSocket<S> {
/// Returns statistics about the current buffer allocation.
///
/// This can be useful for monitoring memory usage per connection.
pub fn buffer_stats(&self) -> BufferStats {
BufferStats {
write_buffer_capacity: self.write_buf.capacity(),
read_buffer_capacity: self.read_buf.capacity(),
}
}
}
2 changes: 1 addition & 1 deletion sqlx-core/src/net/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::Path;
use std::pin::Pin;
use std::task::{ready, Context, Poll};

pub use buffered::{BufferedSocket, WriteBuffer};
pub use buffered::{BufferStats, BufferedSocket, WriteBuffer};
use bytes::BufMut;
use cfg_if::cfg_if;

Expand Down
4 changes: 4 additions & 0 deletions sqlx-mysql/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl AnyConnectionBackend for MySqlConnection {
Connection::shrink_buffers(self);
}

fn buffer_stats(&self) -> Option<sqlx_core::net::BufferStats> {
Connection::buffer_stats(self)
}

fn flush(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> {
Connection::flush(self).boxed()
}
Expand Down
4 changes: 4 additions & 0 deletions sqlx-mysql/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ impl Connection for MySqlConnection {
fn shrink_buffers(&mut self) {
self.inner.stream.shrink_buffers();
}

fn buffer_stats(&self) -> Option<sqlx_core::net::BufferStats> {
Some(self.inner.stream.buffer_stats())
}
}
4 changes: 4 additions & 0 deletions sqlx-postgres/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl AnyConnectionBackend for PgConnection {
Connection::shrink_buffers(self);
}

fn buffer_stats(&self) -> Option<sqlx_core::net::BufferStats> {
Connection::buffer_stats(self)
}

fn flush(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> {
Connection::flush(self).boxed()
}
Expand Down
4 changes: 4 additions & 0 deletions sqlx-postgres/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ impl Connection for PgConnection {
self.inner.stream.shrink_buffers();
}

fn buffer_stats(&self) -> Option<sqlx_core::net::BufferStats> {
Some(self.inner.stream.buffer_stats())
}

#[doc(hidden)]
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
self.wait_until_ready()
Expand Down
Loading