From 7e44afea4980093faa242467eaf3e26ee121f73b Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Fri, 18 Oct 2024 19:48:44 -0700 Subject: [PATCH] breaking(pool): use `usize` for all connection counts --- benches/any/pool.rs | 18 +++++++++++++++--- sqlx-core/src/pool/inner.rs | 8 ++++---- sqlx-core/src/pool/mod.rs | 2 +- sqlx-core/src/pool/options.rs | 12 ++++++------ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/benches/any/pool.rs b/benches/any/pool.rs index a68905805..423b2ce02 100644 --- a/benches/any/pool.rs +++ b/benches/any/pool.rs @@ -9,7 +9,7 @@ use tracing::Instrument; struct Input { threads: usize, tasks: usize, - pool_size: u32, + pool_size: usize, } impl Display for Input { @@ -24,6 +24,7 @@ impl Display for Input { fn bench_pool(c: &mut Criterion) { sqlx::any::install_default_drivers(); + tracing_subscriber::fmt::try_init().ok(); let database_url = dotenvy::var("DATABASE_URL").expect("DATABASE_URL must be set"); @@ -72,6 +73,14 @@ fn bench_pool(c: &mut Criterion) { } fn bench_pool_with(b: &mut Bencher, input: &Input, database_url: &str) { + let _span = tracing::info_span!( + "bench_pool_with", + threads = input.threads, + tasks = input.tasks, + pool_size = input.pool_size + ) + .entered(); + let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() .worker_threads(input.threads) @@ -88,10 +97,13 @@ fn bench_pool_with(b: &mut Bencher, input: &Input, database_url: &str) { .expect("error connecting to pool") }); - for _ in 1..=input.tasks { + for num in 1..=input.tasks { let pool = pool.clone(); - runtime.spawn(async move { while pool.acquire().await.is_ok() {} }); + runtime.spawn( + async move { while pool.acquire().await.is_ok() {} } + .instrument(tracing::info_span!("task", num)), + ); } // Spawn the benchmark loop into the runtime so we're not accidentally including the main thread diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index b698dc9df..7c6a84adf 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -11,7 +11,7 @@ use crate::sync::{AsyncSemaphore, AsyncSemaphoreReleaser}; use std::cmp; use std::future::{self, Future}; use std::pin::pin; -use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, RwLock}; use std::task::Poll; @@ -26,7 +26,7 @@ pub(crate) struct PoolInner { pub(super) connect_options: RwLock::Options>>, pub(super) idle_conns: ArrayQueue>, pub(super) semaphore: AsyncSemaphore, - pub(super) size: AtomicU32, + pub(super) size: AtomicUsize, pub(super) num_idle: AtomicUsize, is_closed: AtomicBool, pub(super) on_closed: event_listener::Event, @@ -55,7 +55,7 @@ impl PoolInner { connect_options: RwLock::new(Arc::new(connect_options)), idle_conns: ArrayQueue::new(capacity), semaphore: AsyncSemaphore::new(options.fair, semaphore_capacity), - size: AtomicU32::new(0), + size: AtomicUsize::new(0), num_idle: AtomicUsize::new(0), is_closed: AtomicBool::new(false), on_closed: event_listener::Event::new(), @@ -71,7 +71,7 @@ impl PoolInner { pool } - pub(super) fn size(&self) -> u32 { + pub(super) fn size(&self) -> usize { self.size.load(Ordering::Acquire) } diff --git a/sqlx-core/src/pool/mod.rs b/sqlx-core/src/pool/mod.rs index f11ff1d76..6e5408948 100644 --- a/sqlx-core/src/pool/mod.rs +++ b/sqlx-core/src/pool/mod.rs @@ -532,7 +532,7 @@ impl Pool { } /// Returns the number of connections currently active. This includes idle connections. - pub fn size(&self) -> u32 { + pub fn size(&self) -> usize { self.0.size() } diff --git a/sqlx-core/src/pool/options.rs b/sqlx-core/src/pool/options.rs index 3d048f179..2c2c2e180 100644 --- a/sqlx-core/src/pool/options.rs +++ b/sqlx-core/src/pool/options.rs @@ -74,12 +74,12 @@ pub struct PoolOptions { + Sync, >, >, - pub(crate) max_connections: u32, + pub(crate) max_connections: usize, pub(crate) acquire_time_level: LevelFilter, pub(crate) acquire_slow_level: LevelFilter, pub(crate) acquire_slow_threshold: Duration, pub(crate) acquire_timeout: Duration, - pub(crate) min_connections: u32, + pub(crate) min_connections: usize, pub(crate) max_lifetime: Option, pub(crate) idle_timeout: Option, pub(crate) fair: bool, @@ -170,13 +170,13 @@ impl PoolOptions { /// Be mindful of the connection limits for your database as well as other applications /// which may want to connect to the same database (or even multiple instances of the same /// application in high-availability deployments). - pub fn max_connections(mut self, max: u32) -> Self { + pub fn max_connections(mut self, max: usize) -> Self { self.max_connections = max; self } /// Get the maximum number of connections that this pool should maintain - pub fn get_max_connections(&self) -> u32 { + pub fn get_max_connections(&self) -> usize { self.max_connections } @@ -202,13 +202,13 @@ impl PoolOptions { /// [`max_lifetime`]: Self::max_lifetime /// [`idle_timeout`]: Self::idle_timeout /// [`max_connections`]: Self::max_connections - pub fn min_connections(mut self, min: u32) -> Self { + pub fn min_connections(mut self, min: usize) -> Self { self.min_connections = min; self } /// Get the minimum number of connections to maintain at all times. - pub fn get_min_connections(&self) -> u32 { + pub fn get_min_connections(&self) -> usize { self.min_connections }