Skip to content

Commit

Permalink
Clean up retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Oct 6, 2023
1 parent 6066b02 commit ce52872
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions buildpacks/ruby/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,28 @@ fn request_container(
req.call().map_err(Box::new)
}

fn time_bounded_retry<T, E, F>(max_time: Duration, sleep_for: Duration, f: F) -> Result<T, E>
where
F: Fn() -> Result<T, E>,
{
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<Response, Box<ureq::Error>> {
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{}",
Expand Down

0 comments on commit ce52872

Please sign in to comment.