Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/swarm-secret-client/src/startup_read.rs

175 lines
6 KiB
Rust

//! The first read of a client secret, retried through a store that is
//! restarting alongside the service reading it.
//!
//! Every service that logs in to the queue reads its secret once at start.
//! The store may still be coming up, so a failed read waits and tries again;
//! a store down for longer than the schedule fails the start, and the unit's
//! `Restart=` tries again.
use std::fmt::Display;
use std::future::Future;
use std::time::Duration;
use backon::{ExponentialBuilder, Retryable as _};
/// The wait after the first failed read.
const FIRST_RETRY: Duration = Duration::from_secs(1);
/// The longest wait between two reads.
const MAX_RETRY: Duration = Duration::from_secs(30);
/// Reads after the first one: waits of 1, 2, 4, 8, 16 and 30 seconds.
const RETRIES: usize = 6;
/// Run `read`, waiting after each failure up to six times, and answer
/// with the first success or the last attempt's error. Each failure is logged
/// as a warning naming `what`.
///
/// This read doesn't hear a stop signal: a caller that already has one in hand
/// uses [`read_with_startup_retry_or_stop`].
///
/// # Errors
/// The error of the final attempt, once every read has failed.
pub async fn read_with_startup_retry<T, E, F, Fut>(what: &str, read: F) -> Result<T, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = Result<T, E>>,
E: Display,
{
read.retry(
ExponentialBuilder::default()
.with_min_delay(FIRST_RETRY)
.with_max_delay(MAX_RETRY)
.with_max_times(RETRIES),
)
.notify(|e: &E, wait| {
tracing::warn!(error = %format!("{e:#}"), retry_in = ?wait, "{what} failed");
})
.await
}
/// [`read_with_startup_retry`], ended by `stop`.
///
/// When `stop` resolves, in a sleep or during an attempt, the read is dropped
/// without another attempt and the answer is `Ok(None)`. The caller exits 0, as
/// it does for the same signal later on, so `systemctl stop` or a pod delete
/// during a store outage returns at once instead of after the schedule (about a
/// minute) or the unit's stop timeout.
///
/// # Errors
/// The error of the final attempt, once every read has failed.
pub async fn read_with_startup_retry_or_stop<T, E, F, Fut>(
what: &str,
stop: impl Future<Output = ()>,
read: F,
) -> Result<Option<T>, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = Result<T, E>>,
E: Display,
{
tokio::select! {
biased;
() = stop => {
tracing::info!("stop arrived while {what}; exiting before startup finished");
Ok(None)
}
read = read_with_startup_retry(what, read) => read.map(Some),
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::time::Instant;
use super::*;
/// A store that fails `failures` times and then answers.
fn read_after(
failures: usize,
calls: &AtomicUsize,
) -> std::future::Ready<Result<&'static str, String>> {
std::future::ready(if calls.fetch_add(1, Ordering::SeqCst) < failures {
Err("store is down".to_owned())
} else {
Ok("secret")
})
}
#[tokio::test(start_paused = true)]
async fn a_store_that_answers_at_once_costs_no_wait() {
let calls = AtomicUsize::new(0);
let started = Instant::now();
let got = read_with_startup_retry("reading", || read_after(0, &calls)).await;
assert_eq!(got, Ok("secret"));
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(started.elapsed(), Duration::ZERO);
}
#[tokio::test(start_paused = true)]
async fn a_store_that_comes_up_late_is_waited_for() {
let calls = AtomicUsize::new(0);
let started = Instant::now();
let got = read_with_startup_retry("reading", || read_after(3, &calls)).await;
assert_eq!(got, Ok("secret"));
assert_eq!(calls.load(Ordering::SeqCst), 4);
assert_eq!(started.elapsed(), Duration::from_secs(1 + 2 + 4));
}
/// Resolves `after` from now, like a SIGTERM that arrives mid-outage.
async fn stop_after(after: Duration) {
tokio::time::sleep(after).await;
}
#[tokio::test(start_paused = true)]
async fn a_stop_during_a_retry_wait_ends_the_read_without_another_attempt() {
// Attempts at 0 s and 1 s fail; the wait after the second runs 1 s to 3 s.
let calls = AtomicUsize::new(0);
let started = Instant::now();
let got =
read_with_startup_retry_or_stop("reading", stop_after(Duration::from_secs(2)), || {
read_after(usize::MAX, &calls)
})
.await;
assert_eq!(got, Ok(None));
assert_eq!(calls.load(Ordering::SeqCst), 2);
assert_eq!(started.elapsed(), Duration::from_secs(2));
}
#[tokio::test(start_paused = true)]
async fn a_stop_that_has_already_fired_makes_no_attempt() {
let calls = AtomicUsize::new(0);
let got = read_with_startup_retry_or_stop("reading", std::future::ready(()), || {
read_after(0, &calls)
})
.await;
assert_eq!(got, Ok(None));
assert_eq!(calls.load(Ordering::SeqCst), 0);
}
#[tokio::test(start_paused = true)]
async fn a_stop_after_the_read_succeeded_changes_nothing() {
let calls = AtomicUsize::new(0);
let got =
read_with_startup_retry_or_stop("reading", stop_after(Duration::from_mins(1)), || {
read_after(1, &calls)
})
.await;
assert_eq!(got, Ok(Some("secret")));
}
#[tokio::test(start_paused = true)]
async fn a_store_that_stays_down_fails_after_the_whole_schedule() {
let calls = AtomicUsize::new(0);
let started = Instant::now();
let got = read_with_startup_retry("reading", || read_after(usize::MAX, &calls)).await;
assert_eq!(got, Err("store is down".to_owned()));
assert_eq!(calls.load(Ordering::SeqCst), RETRIES + 1);
assert_eq!(
started.elapsed(),
Duration::from_secs(1 + 2 + 4 + 8 + 16 + 30)
);
}
}