mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
// HTTP retry timeouts, which take a response error's `Reason`.

use std::time::Duration;

use crate::storage::chrono::Timestamp;
use crate::storage::constants::DEFAULT_RATE_LIMIT_TIMEOUT;
use crate::storage::response::Reason;

/// How long from `now` until `timestamp`; zero once it has passed.
pub(crate) fn until(timestamp: Timestamp, now: Timestamp) -> Duration {
    Duration::from_secs(u64::try_from(timestamp.secs_since_epoch() - now.secs_since_epoch()).unwrap_or(0))
}

/// The longest wait between retries after a run of connection or server
/// errors, so that a resource comes back soon after a long outage ends.
const MAXIMUM_ERROR_BACKOFF: Duration = Duration::from_secs(60);

/// The shortest wait a server's `Retry-After` gets, so that a retry time
/// already past, as a skewed clock makes it, doesn't retry in a loop.
const MINIMUM_RETRY_AFTER: Duration = Duration::from_secs(1);

/// `2^exponent` seconds, at most `MAXIMUM_ERROR_BACKOFF`.
fn backoff(exponent: u32) -> Duration {
    Duration::from_secs(1 << exponent.min(31)).min(MAXIMUM_ERROR_BACKOFF)
}

/// How long to wait, from `now`, before retrying a failed request; `None`
/// when the failure doesn't call for a retry. A server error or rate limit
/// waits as long as the server's `Retry-After` asks.
#[must_use]
pub(crate) fn error_retry_timeout(
    failed_request_reason: Reason,
    failed_requests: u32,
    retry_after: Option<Timestamp>,
    now: Timestamp,
) -> Option<Duration> {
    match (failed_request_reason, retry_after) {
        (Reason::Server | Reason::RateLimit, Some(retry_after)) => {
            Some(until(retry_after, now).max(MINIMUM_RETRY_AFTER))
        }
        // Retry after one second three times, then start exponential backoff.
        (Reason::Server, None) => Some(backoff(failed_requests.saturating_sub(3))),
        // Immediate exponential backoff.
        (Reason::Connection, _) => Some(backoff(failed_requests.saturating_sub(1))),
        (Reason::RateLimit, None) => Some(DEFAULT_RATE_LIMIT_TIMEOUT),
        // No error, or not an error that triggers retries.
        (Reason::Success | Reason::NotFound | Reason::Other, _) => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_failure_waits_by_its_reason_and_count_before_a_retry() {
        let now = Timestamp::from_secs(1_000_000);
        let in_a_minute = Some(Timestamp::from_secs(1_000_060));
        let a_second_ago = Some(Timestamp::from_secs(999_999));
        let secs = |secs| Some(Duration::from_secs(secs));
        for (reason, failed, retry_after, timeout) in [
            (Reason::Success, 1, None, None),
            (Reason::NotFound, 1, None, None),
            (Reason::Other, 1, None, None),
            // Three retries after a second, then a backoff.
            (Reason::Server, 1, None, secs(1)),
            (Reason::Server, 3, None, secs(1)),
            (Reason::Server, 4, None, secs(2)),
            (Reason::Server, 5, None, secs(4)),
            (Reason::Server, 50, None, secs(60)),
            // A backoff from the first.
            (Reason::Connection, 1, None, secs(1)),
            (Reason::Connection, 2, None, secs(2)),
            (Reason::Connection, 4, None, secs(8)),
            (Reason::Connection, 7, None, secs(60)),
            (Reason::Connection, 50, None, secs(60)),
            (Reason::Connection, 1, in_a_minute, secs(1)),
            (Reason::RateLimit, 1, None, secs(5)),
            // As long as `Retry-After` asks, and a second once it passed.
            (Reason::RateLimit, 1, in_a_minute, secs(60)),
            (Reason::Server, 1, in_a_minute, secs(60)),
            (Reason::RateLimit, 1, a_second_ago, secs(1)),
            (Reason::Server, 1, a_second_ago, secs(1)),
        ] {
            assert_eq!(
                error_retry_timeout(reason, failed, retry_after, now),
                timeout,
                "{reason:?} after {failed} failures, retry after {retry_after:?}"
            );
        }
    }
}