Skip to content
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

Polish for fixes from #223 and #224 #226

Merged
merged 9 commits into from
Oct 17, 2024
35 changes: 34 additions & 1 deletion bb8/src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ where
}
}
if let Some(lifetime) = config.max_lifetime {
if now - conn.conn.birth >= lifetime {
if conn.conn.is_expired(now, lifetime) {
closed_max_lifetime += 1;
keep &= false;
}
Expand Down Expand Up @@ -252,6 +252,11 @@ impl<C: Send> Conn<C> {
birth: Instant::now(),
}
}

pub(crate) fn is_expired(&self, now: Instant, max: Duration) -> bool {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, your earlier formulation of this failed on Windows due to an overflow. You changed now - conn.conn.birth >= lifetime to (effectively) self.birth <= (now - lifetime), but this failed CI on Windows with overflow when subtracting duration from instant on line 155 (which did the now - lifetime substraction). Changed this to still extract the method, but take 2 arguments so we can keep the earlier formulation.

// The current age of the connection is longer than the maximum allowed lifetime
now - self.birth >= max
}
}

impl<C: Send> From<IdleConn<C>> for Conn<C> {
Expand Down Expand Up @@ -357,3 +362,31 @@ pub(crate) enum StatsKind {
ClosedBroken,
ClosedInvalid,
}

#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};

use crate::internals::Conn;

#[test]
fn test_conn_is_expired() {
let conn = Conn {
conn: (),
birth: Instant::now(),
};

assert!(
!conn.is_expired(conn.birth, Duration::from_nanos(1)),
"at birth, the connection has not expired"
);
assert!(
!conn.is_expired(Instant::now(), Duration::from_secs(5)),
"from setup to now is shorter than 5s, so the connection has not expired"
);
assert!(
conn.is_expired(Instant::now(), Duration::from_nanos(1)),
"from setup to now is longer than 1ns, so the connection has expired"
);
}
}