Skip to content

Commit 34ce2d7

Browse files
committed
Clamp zero connection count
1 parent 77faf23 commit 34ce2d7

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/pool.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct PoolBuilder {
3737
flags: OpenFlags,
3838
journal_mode: Option<JournalMode>,
3939
vfs: Option<String>,
40-
num_conns: Option<NonZeroUsize>,
40+
num_conns: Option<usize>,
4141
}
4242

4343
impl PoolBuilder {
@@ -78,22 +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`.
8283
///
8384
/// ```
8485
/// use async_sqlite::PoolBuilder;
85-
/// use std::num::NonZeroUsize;
8686
///
87-
/// let builder = PoolBuilder::new().num_conns(NonZeroUsize::new(2).unwrap());
87+
/// let builder = PoolBuilder::new().num_conns(2);
8888
/// ```
89-
///
90-
/// ```compile_fail
91-
/// use async_sqlite::PoolBuilder;
92-
///
93-
/// PoolBuilder::new().num_conns(0);
94-
/// ```
95-
pub fn num_conns(mut self, num_conns: NonZeroUsize) -> Self {
96-
self.num_conns = Some(num_conns);
89+
pub fn num_conns(mut self, num_conns: usize) -> Self {
90+
self.num_conns = Some(num_conns.max(1));
9791
self
9892
}
9993

@@ -110,7 +104,7 @@ impl PoolBuilder {
110104
/// ```
111105
pub async fn open(self) -> Result<Pool, Error> {
112106
let num_conns = self.get_num_conns();
113-
let opens = (0..num_conns.get()).map(|_| {
107+
let opens = (0..num_conns).map(|_| {
114108
ClientBuilder {
115109
path: self.path.clone(),
116110
flags: self.flags,
@@ -145,7 +139,7 @@ impl PoolBuilder {
145139
/// ```
146140
pub fn open_blocking(self) -> Result<Pool, Error> {
147141
let num_conns = self.get_num_conns();
148-
let clients = (0..num_conns.get())
142+
let clients = (0..num_conns)
149143
.map(|_| {
150144
ClientBuilder {
151145
path: self.path.clone(),
@@ -164,8 +158,8 @@ impl PoolBuilder {
164158
})
165159
}
166160

167-
fn get_num_conns(&self) -> NonZeroUsize {
168-
self.num_conns.unwrap_or_else(|| {
161+
fn get_num_conns(&self) -> usize {
162+
.unwrap_or_else(|| available_parallelism().unwrap_or_else(|_| NonZeroUsize::new(1).unwrap()).into())
169163
available_parallelism().unwrap_or_else(|_| NonZeroUsize::new(1).unwrap())
170164
})
171165
}

tests/tests.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use async_sqlite::{ClientBuilder, Error, JournalMode, PoolBuilder};
2-
use std::num::NonZeroUsize;
32

43
#[test]
54
fn test_blocking_client() {
@@ -85,6 +84,7 @@ async_test!(test_journal_mode);
8584
async_test!(test_concurrency);
8685
async_test!(test_pool);
8786
async_test!(test_pool_conn_for_each);
87+
async_test!(test_pool_num_conns_zero_clamps);
8888

8989
async fn test_journal_mode() {
9090
let tmp_dir = tempfile::tempdir().unwrap();
@@ -139,7 +139,7 @@ async fn test_pool() {
139139
let tmp_dir = tempfile::tempdir().unwrap();
140140
let pool = PoolBuilder::new()
141141
.path(tmp_dir.path().join("sqlite.db"))
142-
.num_conns(NonZeroUsize::new(2).unwrap())
142+
.num_conns(2)
143143
.open()
144144
.await
145145
.expect("client unable to be opened");
@@ -192,7 +192,7 @@ async fn test_pool_conn_for_each() {
192192

193193
let pool = PoolBuilder::new()
194194
.path(tmp_dir.path().join("another-sqlite.db"))
195-
.num_conns(NonZeroUsize::new(2).unwrap())
195+
.num_conns(2)
196196
.open()
197197
.await
198198
.expect("pool unable to be opened");
@@ -228,3 +228,15 @@ async fn test_pool_conn_for_each() {
228228
// cleanup
229229
pool.close().await.expect("closing client conn");
230230
}
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)