Skip to content

Fix PoolConstraints infinite wait when the max value Is zero #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
21 changes: 17 additions & 4 deletions src/conn/opts/pool_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ const_assert!(
PoolConstraints::DEFAULT.min <= PoolConstraints::DEFAULT.max,
);

const_assert!(
_POOL_CONSTRAINTS_MIN_IS_NONZERO,
PoolConstraints::DEFAULT.min > 0
);

const_assert!(
_POOL_CONSTRAINTS_MAX_IS_NONZERO,
PoolConstraints::DEFAULT.max > 0
);

pub struct Assert<const L: usize, const R: usize>;
impl<const L: usize, const R: usize> Assert<L, R> {
pub const LEQ: usize = R - L;
Expand Down Expand Up @@ -169,15 +179,18 @@ impl PoolConstraints {
/// # Ok(()) }
/// ```
pub fn new(min: usize, max: usize) -> Option<PoolConstraints> {
if min <= max {
Some(PoolConstraints { min, max })
} else {
None
match (min, max) {
(0, 0) => None,
(min, max) if min <= max => Some(PoolConstraints { min, max }),
_ => None,
}
}

pub const fn new_const<const MIN: usize, const MAX: usize>() -> PoolConstraints {
gte::<MIN, MAX>();

assert!(MAX > 0);

PoolConstraints { min: MIN, max: MAX }
}

Expand Down
12 changes: 12 additions & 0 deletions src/conn/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ mod test {
assert!(pool.try_get_conn(Duration::from_millis(357)).is_ok());
}

#[test]
fn should_be_none_if_pool_size_zero_zero() {
let pool_constraints = PoolConstraints::new(0, 0);
assert!(pool_constraints.is_none());
}

#[test]
#[should_panic]
fn should_panic_if_pool_size_zero_zero() {
PoolConstraints::new_const::<0, 0>();
}

#[test]
fn should_execute_statements_on_PooledConn() {
let pool = Pool::new(get_opts()).unwrap();
Expand Down
Loading