-
Notifications
You must be signed in to change notification settings - Fork 94
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
ref(projects): Jitter project state requests #277
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,10 @@ impl ProjectConfig { | |
} | ||
} | ||
|
||
fn project_state_seed() -> f64 { | ||
rand::random() | ||
} | ||
|
||
/// The project state is a cached server state of a project. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
|
@@ -277,6 +281,9 @@ pub struct ProjectState { | |
/// The organization id. | ||
#[serde(default)] | ||
pub organization_id: Option<u64>, | ||
/// Random seed for jitter and related operations. | ||
#[serde(skip, default = "project_state_seed")] | ||
pub seed: f64, | ||
} | ||
|
||
impl ProjectState { | ||
|
@@ -291,6 +298,7 @@ impl ProjectState { | |
config: Default::default(), | ||
rev: None, | ||
organization_id: None, | ||
seed: project_state_seed(), | ||
} | ||
} | ||
|
||
|
@@ -334,11 +342,15 @@ impl ProjectState { | |
|
||
/// Returns whether this state is outdated and needs to be refetched. | ||
pub fn outdated(&self, config: &Config) -> bool { | ||
// Jitter is used to compute a random interval between [0; 2 * expiry_interval] | ||
let jitter = config.cache_timeout_jitter(); | ||
let factor = jitter * (self.seed * 2.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Double check this when less tired. Math is hard.
|
||
|
||
SystemTime::from(self.last_fetch) | ||
.elapsed() | ||
.map(|e| match self.slug { | ||
Some(_) => e > config.project_cache_expiry(), | ||
None => e > config.cache_miss_expiry(), | ||
Some(_) => e > config.project_cache_expiry().mul_f64(factor), | ||
None => e > config.cache_miss_expiry().mul_f64(factor), | ||
}) | ||
.unwrap_or(false) | ||
} | ||
|
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.
Is the intent that the dice are rolled once per project over the uptime of a relay, not once per fetch?
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.
The intent is once per fetch (as stated in the description):
... when Relay has long uptimes.