-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow --metadata-ttl without --cache and set default with --cache to …
…60s (#855) * Allow --metadata-ttl without --cache and set default with --cache to 60s Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> * PR feedback Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> * Show 0 TTL warning in background mode Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> * Update docs and changelog Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> * Colorize warning with owo_colors Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> * Break items in the changelog Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk> --------- Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk>
- Loading branch information
Showing
9 changed files
with
165 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{fmt::Display, num::ParseIntError, str::FromStr, time::Duration}; | ||
|
||
use thiserror::Error; | ||
|
||
/// User-configurable time-to-live (TTL) for metadata caching. | ||
#[derive(Debug, Clone, Copy)] | ||
pub enum TimeToLive { | ||
Minimal, | ||
Indefinite, | ||
Duration(Duration), | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum TimeToLiveError { | ||
#[error("TTL must be a valid number of seconds, or 'indefinite', or 'minimal'")] | ||
InvalidInt(#[from] ParseIntError), | ||
#[error( | ||
"TTL must not be greater than {}s (~{} years), or be 'indefinite', or 'minimal'", | ||
TimeToLive::MAXIMUM_TTL_SECONDS, | ||
TimeToLive::MAXIMUM_TTL_YEARS | ||
)] | ||
TooLarge, | ||
} | ||
|
||
impl TimeToLive { | ||
const MINIMAL: &'static str = "minimal"; | ||
const INDEFINITE: &'static str = "indefinite"; | ||
|
||
// Set an upper bound that is practically "forever", but does not cause overflow | ||
// when added to `Instant::now()` (as `u64::MAX` would). | ||
const MAXIMUM_TTL_YEARS: u64 = 100; | ||
const MAXIMUM_TTL_SECONDS: u64 = Self::MAXIMUM_TTL_YEARS * 365 * 24 * 60 * 60; | ||
|
||
pub const INDEFINITE_DURATION: Duration = Duration::from_secs(Self::MAXIMUM_TTL_SECONDS); | ||
|
||
pub fn new_from_str(s: &str) -> Result<Self, TimeToLiveError> { | ||
match s { | ||
Self::MINIMAL => Ok(Self::Minimal), | ||
Self::INDEFINITE => Ok(Self::Indefinite), | ||
_ => { | ||
let seconds = s.parse()?; | ||
if seconds > Self::MAXIMUM_TTL_SECONDS { | ||
return Err(TimeToLiveError::TooLarge); | ||
} | ||
|
||
let duration = Duration::from_secs(seconds); | ||
Ok(Self::Duration(duration)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Display for TimeToLive { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Minimal => f.write_str(Self::MINIMAL), | ||
Self::Indefinite => f.write_str(Self::INDEFINITE), | ||
Self::Duration(duration) => write!(f, "{}s", duration.as_secs()), | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for TimeToLive { | ||
type Err = TimeToLiveError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Self::new_from_str(s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters