Skip to content

Commit 4224d5b

Browse files
committed
Clamp number of pool connections to 1
1 parent a987eae commit 4224d5b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/pool.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ impl PoolBuilder {
7878

7979
/// Specify the number of sqlite connections to open as part of the pool.
8080
///
81-
/// Defaults to the number of logical CPUs of the current system.
81+
/// Defaults to the number of logical CPUs of the current system. Values
82+
/// less than `1` are clamped to `1`.
83+
///
84+
/// ```
85+
/// use async_sqlite::PoolBuilder;
86+
///
87+
/// let builder = PoolBuilder::new().num_conns(2);
88+
/// ```
8289
pub fn num_conns(mut self, num_conns: usize) -> Self {
83-
self.num_conns = Some(num_conns);
90+
self.num_conns = Some(num_conns.max(1));
8491
self
8592
}
8693

tests/tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ async_test!(test_journal_mode);
8484
async_test!(test_concurrency);
8585
async_test!(test_pool);
8686
async_test!(test_pool_conn_for_each);
87+
async_test!(test_pool_num_conns_zero_clamps);
8788

8889
async fn test_journal_mode() {
8990
let tmp_dir = tempfile::tempdir().unwrap();
@@ -227,3 +228,15 @@ async fn test_pool_conn_for_each() {
227228
// cleanup
228229
pool.close().await.expect("closing client conn");
229230
}
231+
232+
async fn test_pool_num_conns_zero_clamps() {
233+
let tmp_dir = tempfile::tempdir().unwrap();
234+
let pool = PoolBuilder::new()
235+
.path(tmp_dir.path().join("clamp.db"))
236+
.num_conns(0)
237+
.open()
238+
.await
239+
.expect("pool unable to be opened");
240+
let results = pool.conn_for_each(|_| Ok(())).await;
241+
assert_eq!(results.len(), 1);
242+
}

0 commit comments

Comments
 (0)