From ce52872e32946f3908bb9d09306cb9de27358c06 Mon Sep 17 00:00:00 2001 From: Richard Schneeman Date: Tue, 3 Oct 2023 15:50:45 -0500 Subject: [PATCH] Clean up retry logic --- buildpacks/ruby/tests/integration_test.rs | 40 ++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/buildpacks/ruby/tests/integration_test.rs b/buildpacks/ruby/tests/integration_test.rs index ea8ef0c3..740330a8 100644 --- a/buildpacks/ruby/tests/integration_test.rs +++ b/buildpacks/ruby/tests/integration_test.rs @@ -167,32 +167,28 @@ fn request_container( req.call().map_err(Box::new) } +fn time_bounded_retry(max_time: Duration, sleep_for: Duration, f: F) -> Result +where + F: Fn() -> Result, +{ + let start = Instant::now(); + + loop { + let result = f(); + if result.is_ok() || max_time <= (start.elapsed() + sleep_for) { + return result; + } + thread::sleep(sleep_for); + } +} + fn call_root_until_boot( container: &ContainerContext, port: u16, ) -> Result> { - let mut count = 0; - let max_time = 10.0_f64; // Seconds - let sleep_for = 0.1_f64; - - #[allow(clippy::cast_possible_truncation)] - let max_count = (max_time / sleep_for).floor() as i64; - let mut response = request_container(container, port, ""); - while count < max_count { - count += 1; - match response { - Err(ref box_e) => match box_e.as_ref() { - ureq::Error::Transport(error) => { - println!("Waiting for connection {error}, retrying in {sleep_for}"); - response = request_container(container, port, ""); - } - ureq::Error::Status(..) => break, - }, - _ => break, - } - - thread::sleep(frac_seconds(sleep_for)); - } + let response = time_bounded_retry(Duration::from_secs(10), frac_seconds(0.1_f64), || { + request_container(container, port, "") + }); println!( "{}\n{}",