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 3, 2023
1 parent 751a363 commit 64c7e62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
3 changes: 1 addition & 2 deletions buildpacks/ruby/src/rake_task_detect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use commons::fun_run::{CmdError, CommandWithName};
#[allow(clippy::wildcard_imports)]
use commons::output::{fmt, section_log::*};

use commons::fun_run::{CmdError, CommandWithName};
use core::str::FromStr;
use std::{ffi::OsStr, process::Command};

Expand Down
44 changes: 22 additions & 22 deletions buildpacks/ruby/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,32 @@ fn request_container(
req.call().map_err(Box::new)
}

fn time_bounded_retry<T, E>(
max_time: Duration,
sleep_for: Duration,
f: impl Fn() -> Result<T, E>,
) -> Result<T, E> {
let start = Instant::now();

let mut result = f();
while start.elapsed() < max_time {
if result.is_ok() {
return result;
}

thread::sleep(sleep_for);
result = f();
}
result
}

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 64c7e62

Please sign in to comment.