-
Notifications
You must be signed in to change notification settings - Fork 260
feat(ads-client): respect server max-age in cache TTL resolution #7344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Almaju
wants to merge
6
commits into
mozilla:main
Choose a base branch
from
Almaju:ac-103-cache-ttl-strategy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aec4860
feat(ads-client): respect server max-age in cache TTL resolution
Almaju 3566183
refactor(ads-client): extract TTL resolution from CacheControl
Almaju fe6fb4c
refactor(ads-client): make resolve a method on TtlInputs
Almaju 32517f3
refactor(ads-client): remove TTL/size bounds from http_cache
Almaju 1f03968
refactor(ads-client): rename TtlInputs to EffectiveTtl
Almaju fc40827
revert(ads-client): restore builder validation and telemetry mappings
Almaju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,82 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| /// The TTL to use when storing a response in the cache, computed from | ||
| /// three possible sources. | ||
| /// | ||
| /// `explicit` comes from the caller, `server_max_age` from the response's | ||
| /// `Cache-Control` header, and `default` from the cache's configuration. | ||
| pub struct EffectiveTtl { | ||
| /// Per-request override provided by the caller, if any. | ||
| pub explicit: Option<Duration>, | ||
| /// `Cache-Control: max-age` from the server response, if present. | ||
| pub server_max_age: Option<Duration>, | ||
| /// The cache's configured default TTL. | ||
| pub default: Duration, | ||
| } | ||
|
|
||
| impl EffectiveTtl { | ||
| /// Resolve the TTL by priority (highest to lowest): | ||
| /// 1. `explicit` — caller-provided per-request override. | ||
| /// 2. `server_max_age` — value of `Cache-Control: max-age` on the response. | ||
| /// 3. `default` — the cache's configured default. | ||
| pub fn resolve(&self) -> Duration { | ||
| self.explicit | ||
| .or(self.server_max_age) | ||
| .unwrap_or(self.default) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn explicit_overrides_server_max_age_and_default() { | ||
| let ttl = EffectiveTtl { | ||
| explicit: Some(Duration::from_secs(60)), | ||
| server_max_age: Some(Duration::from_secs(3600)), | ||
| default: Duration::from_secs(300), | ||
| } | ||
| .resolve(); | ||
| assert_eq!(ttl, Duration::from_secs(60)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn falls_back_to_server_max_age_when_no_explicit() { | ||
| let ttl = EffectiveTtl { | ||
| explicit: None, | ||
| server_max_age: Some(Duration::from_secs(3600)), | ||
| default: Duration::from_secs(300), | ||
| } | ||
| .resolve(); | ||
| assert_eq!(ttl, Duration::from_secs(3600)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn falls_back_to_default_when_no_explicit_and_no_server_max_age() { | ||
| let ttl = EffectiveTtl { | ||
| explicit: None, | ||
| server_max_age: None, | ||
| default: Duration::from_secs(300), | ||
| } | ||
| .resolve(); | ||
| assert_eq!(ttl, Duration::from_secs(300)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn zero_server_max_age_yields_zero() { | ||
| // Lets the strategy emit NoCache without a network round-trip. | ||
| let ttl = EffectiveTtl { | ||
| explicit: None, | ||
| server_max_age: Some(Duration::ZERO), | ||
| default: Duration::from_secs(300), | ||
| } | ||
| .resolve(); | ||
| assert_eq!(ttl, Duration::ZERO); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall implementation looks good! However, the description says the ttl is capped at 7 days max, but this resolve function is not performing any capping.
Seems like the capping was removed at some point, was that intentional?