Skip to content

Commit

Permalink
Merge pull request #11 from niclabs/timeout
Browse files Browse the repository at this point in the history
timeouts values
  • Loading branch information
Ephyy authored May 30, 2024
2 parents 049c228 + aaf6cd3 commit 2e5affb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/async_resolver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl ResolverConfig {
let retransmission_loop_attempts = 3;
let global_retransmission_limit = 30;
let timeout = Duration::from_secs(45);
let max_retry_interval_seconds = 10;
let max_retry_interval_seconds = 60;

let mut servers_info = Vec::new();
servers_info.push(ServerInfo::new_from_addr(GOOGLE_PRIMARY_DNS_SERVER.into(), timeout));
Expand Down Expand Up @@ -423,7 +423,7 @@ mod tests_resolver_config {
fn get_and_set_max_retry_interval_seconds() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_max_retry_interval_seconds(), 10);
assert_eq!(resolver_config.get_max_retry_interval_seconds(), 60);

resolver_config.set_max_retry_interval_seconds(20);

Expand Down
38 changes: 30 additions & 8 deletions src/async_resolver/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::lookup_response::LookupResponse;
use super::resolver_error::ResolverError;
use super::server_info::ServerInfo;
use std::sync::{Mutex,Arc};
use std::time::Instant;
use crate::client::client_connection::ConnectionProtocol;
use crate::async_resolver::config::ResolverConfig;

Expand Down Expand Up @@ -53,11 +54,23 @@ impl LookupStrategy {
let config: &ResolverConfig = &self.config;
let upper_limit_of_retransmission_loops: u16 = config.get_retransmission_loop_attempts();
let max_interval: u64 = config.get_max_retry_interval_seconds();
let start_interval: u64 = config.get_min_retry_interval_seconds();
let initial_rto = 1.0;
let mut rto = initial_rto;
let mut srtt = rto;
let mut rttvar = rto/2.0;

let mut interval: u64 = start_interval;
let mut timeout_duration = tokio::time::Duration::from_secs(interval);
let mut timeout_duration = tokio::time::Duration::from_secs_f64(rto);
let mut lookup_response_result: Result<LookupResponse, ResolverError> = Err(ResolverError::EmptyQuery);
let start = Instant::now();
let mut end = start;

// Incrementar end hasta que cambie
while end == start {
end = Instant::now();
}

let granularity = end.duration_since(start).as_secs_f64() + end.duration_since(start).subsec_nanos() as f64 * 1e-9;


// The resolver cycles through servers and at the end of a cycle, backs off
// the timeout exponentially.
Expand All @@ -67,18 +80,27 @@ impl LookupStrategy {
let mut servers_iter = servers_to_query.iter();

while let Some(server_info) = servers_iter.next() {
//start timer
let start = Instant::now();
lookup_response_result = self.transmit_query_to_server(
server_info,
timeout_duration
).await;
if self.received_appropriate_response() {break 'global_cycle}
//end timer
let end = Instant::now();

let rtt = end.duration_since(start);
rttvar = (1.0 - 0.25) * rttvar + 0.25 * (rtt.as_secs_f64() - srtt).abs();
srtt = (1.0 - 0.125) * srtt + 0.125 * rtt.as_secs_f64();
rto = srtt + granularity.max(4.0 * rttvar) ;
timeout_duration = tokio::time::Duration::from_secs_f64(rto);
if self.received_appropriate_response() { break 'global_cycle }
}

// Exponencial backoff
if interval < max_interval {
interval = interval*2;
}
timeout_duration = tokio::time::Duration::from_secs(interval);

rto = (rto * 2.0).min(max_interval as f64);
timeout_duration = tokio::time::Duration::from_secs_f64(rto);
tokio::time::sleep(timeout_duration).await;
}
return lookup_response_result;
Expand Down

0 comments on commit 2e5affb

Please sign in to comment.