Skip to content

Commit eaf3ff9

Browse files
authored
Merge pull request #398 from powerumc/issue-397
Fix PoolConstraints infinite wait when the max value Is zero
2 parents 6d7128e + 8396488 commit eaf3ff9

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/conn/opts/pool_opts.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ const_assert!(
140140
PoolConstraints::DEFAULT.min <= PoolConstraints::DEFAULT.max,
141141
);
142142

143+
const_assert!(
144+
_POOL_CONSTRAINTS_MIN_IS_NONZERO,
145+
PoolConstraints::DEFAULT.min > 0
146+
);
147+
148+
const_assert!(
149+
_POOL_CONSTRAINTS_MAX_IS_NONZERO,
150+
PoolConstraints::DEFAULT.max > 0
151+
);
152+
143153
pub struct Assert<const L: usize, const R: usize>;
144154
impl<const L: usize, const R: usize> Assert<L, R> {
145155
pub const LEQ: usize = R - L;
@@ -169,15 +179,18 @@ impl PoolConstraints {
169179
/// # Ok(()) }
170180
/// ```
171181
pub fn new(min: usize, max: usize) -> Option<PoolConstraints> {
172-
if min <= max {
173-
Some(PoolConstraints { min, max })
174-
} else {
175-
None
182+
match (min, max) {
183+
(0, 0) => None,
184+
(min, max) if min <= max => Some(PoolConstraints { min, max }),
185+
_ => None,
176186
}
177187
}
178188

179189
pub const fn new_const<const MIN: usize, const MAX: usize>() -> PoolConstraints {
180190
gte::<MIN, MAX>();
191+
192+
assert!(MAX > 0);
193+
181194
PoolConstraints { min: MIN, max: MAX }
182195
}
183196

src/conn/pool/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,18 @@ mod test {
508508
assert!(pool.try_get_conn(Duration::from_millis(357)).is_ok());
509509
}
510510

511+
#[test]
512+
fn should_be_none_if_pool_size_zero_zero() {
513+
let pool_constraints = PoolConstraints::new(0, 0);
514+
assert!(pool_constraints.is_none());
515+
}
516+
517+
#[test]
518+
#[should_panic]
519+
fn should_panic_if_pool_size_zero_zero() {
520+
PoolConstraints::new_const::<0, 0>();
521+
}
522+
511523
#[test]
512524
fn should_execute_statements_on_PooledConn() {
513525
let pool = Pool::new(get_opts()).unwrap();

0 commit comments

Comments
 (0)