diff --git a/crates/openshell-extension-core/src/transport.rs b/crates/openshell-extension-core/src/transport.rs index 8895b7f261..07746c99f7 100644 --- a/crates/openshell-extension-core/src/transport.rs +++ b/crates/openshell-extension-core/src/transport.rs @@ -203,10 +203,12 @@ fn call_was_not_processed(status: &Status) -> bool { /// Failures with an unknown outcome and every answer from the service are /// returned as they are. /// -/// `call` builds and sends one attempt. No attempt starts after `deadline`, -/// or after a fixed window when there is none, and the failure of the last -/// attempt is returned. An attempt still running at `deadline` is the -/// caller's to cut short. +/// `call` builds and sends one attempt. A retry starts only if it leaves at +/// least the pause it waited to answer before `deadline` (or before a fixed +/// window when there is none), so a service that keeps refusing the call is +/// reported as such and not as one that timed out. The failure of the last +/// attempt is returned. An attempt still running at `deadline` is the caller's +/// to cut short. pub async fn call_with_retries( deadline: Option, mut call: F, @@ -223,7 +225,8 @@ where Ok(response) => return Ok(response), Err(status) => status, }; - if !call_was_not_processed(&status) || tokio::time::Instant::now() + pause > give_up { + // The retry starts one `pause` from now and keeps one more to answer. + if !call_was_not_processed(&status) || tokio::time::Instant::now() + 2 * pause > give_up { return Err(status); } tokio::time::sleep(pause).await; @@ -527,7 +530,7 @@ mod tests { assert!(call_was_not_processed(&result.unwrap_err())); let elapsed = started.elapsed(); assert!(elapsed <= UNPROCESSED_RETRY_WINDOW); - assert!(elapsed + UNPROCESSED_RETRY_MAX_PAUSE > UNPROCESSED_RETRY_WINDOW); + assert!(elapsed + 2 * UNPROCESSED_RETRY_MAX_PAUSE > UNPROCESSED_RETRY_WINDOW); } #[tokio::test(start_paused = true)] @@ -542,10 +545,106 @@ mod tests { assert!(status.to_string().contains("connect failure"), "{status}"); let elapsed = started.elapsed(); assert!(elapsed <= timeout); - assert!(elapsed + UNPROCESSED_RETRY_MAX_PAUSE > timeout); + assert!(elapsed + 2 * UNPROCESSED_RETRY_MAX_PAUSE > timeout); } } + /// With instant refusals the retries would run at 0, 5, 15, 35, 75, 155, + /// 255 and 355 ms. The 355 ms retry is not started, because it would have + /// 1 ms to answer before the 356 ms deadline: a refusal that takes 5 ms to + /// arrive would overrun it and read as a timeout. + #[tokio::test(start_paused = true)] + async fn a_retry_started_just_before_the_deadline_returns_the_transport_failure() { + let start = tokio::time::Instant::now(); + let deadline = start + Duration::from_millis(356); + let result: Result<(), Status> = tokio::time::timeout_at( + deadline, + call_with_retries(Some(deadline), || async move { + if start.elapsed() >= Duration::from_millis(300) { + tokio::time::sleep(Duration::from_millis(5)).await; + } + Err(connect_failure(io::ErrorKind::ConnectionRefused)) + }), + ) + .await + .expect("timed out instead of reporting the transport failure"); + assert!(call_was_not_processed(&result.unwrap_err())); + } + + /// A retry that connected and then hangs is a slow service, not a + /// refusal: the caller's timeout reports it, as it does for a first attempt. + #[tokio::test(start_paused = true)] + async fn a_retry_that_hangs_is_left_to_the_caller() { + let deadline = tokio::time::Instant::now() + Duration::from_millis(500); + let mut attempts = 0; + let result = tokio::time::timeout_at( + deadline, + call_with_retries(Some(deadline), || { + attempts += 1; + let hangs = attempts > 1; + async move { + if hangs { + std::future::pending::<()>().await; + } + Err::<(), _>(connect_failure(io::ErrorKind::ConnectionRefused)) + } + }), + ) + .await; + assert!(result.is_err()); + assert_eq!(attempts, 2); + } + + /// Without a deadline the window only limits when a retry starts. + #[tokio::test(start_paused = true)] + async fn a_retry_started_inside_the_window_may_answer_after_it() { + let mut attempts = 0; + let answer = call_with_retries(None, || { + attempts += 1; + let slow = attempts > 1; + async move { + if slow { + tokio::time::sleep(UNPROCESSED_RETRY_WINDOW + Duration::from_secs(1)).await; + Ok("answer") + } else { + Err(connect_failure(io::ErrorKind::ConnectionRefused)) + } + } + }) + .await + .unwrap(); + assert_eq!(answer, "answer"); + } + + /// A backoff that leaves no room to answer starts no retry. + #[tokio::test(start_paused = true)] + async fn no_attempt_starts_once_the_backoff_reaches_the_deadline() { + let deadline = tokio::time::Instant::now() + UNPROCESSED_RETRY_FIRST_PAUSE; + let mut attempts = 0; + let result: Result<(), Status> = call_with_retries(Some(deadline), || { + attempts += 1; + async { Err(connect_failure(io::ErrorKind::ConnectionRefused)) } + }) + .await; + assert!(call_was_not_processed(&result.unwrap_err())); + assert_eq!(attempts, 1); + } + + /// The first attempt is not bounded here: a slow service is a timeout. + #[tokio::test(start_paused = true)] + async fn a_first_attempt_running_at_the_deadline_is_left_to_the_caller() { + let deadline = tokio::time::Instant::now() + Duration::from_millis(500); + let result = tokio::time::timeout_at( + deadline, + call_with_retries(Some(deadline), || async { + tokio::time::sleep(Duration::from_secs(10)).await; + Err::<(), _>(connect_failure(io::ErrorKind::ConnectionRefused)) + }), + ) + .await; + assert!(result.is_err()); + } + #[test] fn a_stream_error_this_side_raised_does_not_prove_the_call_was_not_processed() { for reason in [