-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathminimum_ttl.rs
More file actions
43 lines (36 loc) · 1.45 KB
/
minimum_ttl.rs
File metadata and controls
43 lines (36 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Minimum TTL enforcement.
//!
//! This module contains functions to enforce a lower-bound for TTL's (including negative TTL's
//! used during error recovery) to prevent DNS resolution from spinning in a hot-loop.
use tokio::time::{Duration, Instant};
use tracing::debug;
/// The minimum TTL duration that will be respected.
const MINIMUM_TTL: Duration = Duration::from_secs(5);
/// Apply a lower-bound to the given [`Instant`].
///
/// NB: This enforces a lower-bound for TTL's to prevent DNS resolution from spinning in a
/// hot-loop.
#[tracing::instrument(level = "debug")]
pub fn with_minimum_expiry(valid_until: Instant) -> Instant {
let minimum = Instant::now() + MINIMUM_TTL;
// Choose a deadline; if the expiry is too short, fall back to the minimum TTL.
let deadline = if valid_until >= minimum {
valid_until
} else {
debug!(ttl.min = ?MINIMUM_TTL, "Given TTL too short, using a minimum TTL");
minimum
};
deadline
}
/// Apply a lower-bound to the given [`Duration`].
///
/// NB: This enforces a lower-bound for negative TTL's to prevent DNS resolution error recovery
/// from spinning in a hot-loop.
pub fn with_minimum_duration(ttl: Duration) -> Duration {
if ttl < MINIMUM_TTL {
// Choose a deadline; if the expiry is too short, fall back to the minimum TTL.
debug!(ttl.min = ?MINIMUM_TTL, ?ttl, "Given Negative TTL too short, using a minimum TTL");
return MINIMUM_TTL;
}
ttl
}