Skip to content

Commit

Permalink
Merge branch 'main' into spofford/rsq
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity authored Oct 17, 2023
2 parents c3d4752 + daf0035 commit 2af59a1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
19 changes: 13 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ic-agent/src/agent/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl AgentBuilder {
/// Provides a _default_ ingress expiry. This is the delta that will be applied
/// at the time an update or query is made. The default expiry cannot be a
/// fixed system time.
///
/// The timestamp corresponding to this duration may be rounded in order to reduce
/// cache misses. The current implementation rounds to the nearest minute if the
/// expiry is more than a minute, but this is not guaranteed.
pub fn with_ingress_expiry(mut self, ingress_expiry: Option<std::time::Duration>) -> Self {
self.config.ingress_expiry = ingress_expiry;
self
Expand Down
43 changes: 37 additions & 6 deletions ic-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,15 @@ impl Agent {
}

fn get_expiry_date(&self) -> u64 {
// TODO(hansl): evaluate if we need this on the agent side (my hunch is we don't).
let permitted_drift = Duration::from_secs(60);
self.ingress_expiry
.as_nanos()
.saturating_add(OffsetDateTime::now_utc().unix_timestamp_nanos() as u128)
.saturating_sub(permitted_drift.as_nanos()) as u64
let expiry_raw = OffsetDateTime::now_utc() + self.ingress_expiry;
let mut rounded = expiry_raw.replace_nanosecond(0).unwrap();
if self.ingress_expiry.as_secs() > 60 {
rounded = rounded.replace_second(0).unwrap();
if expiry_raw.second() >= 30 {
rounded += Duration::from_secs(60);
}
}
rounded.unix_timestamp_nanos() as u64
}

/// Return the principal of the identity.
Expand Down Expand Up @@ -1447,3 +1450,31 @@ impl<'agent> UpdateBuilder<'agent> {
})
}
}

#[cfg(all(test, feature = "reqwest"))]
mod offline_tests {
use super::*;
// Any tests that involve the network should go in agent_test, not here.

#[test]
fn rounded_expiry() {
let agent = Agent::builder()
.with_url("http://not-a-real-url")
.build()
.unwrap();
let mut prev_expiry = None;
let mut num_timestamps = 0;
for _ in 0..6 {
let update = agent
.update(&Principal::management_canister(), "not_a_method")
.sign()
.unwrap();
if prev_expiry < Some(update.ingress_expiry) {
prev_expiry = Some(update.ingress_expiry);
num_timestamps += 1;
}
}
// in six requests, there should be no more than two timestamps
assert!(num_timestamps <= 2, "num_timestamps:{num_timestamps} > 2");
}
}

0 comments on commit 2af59a1

Please sign in to comment.