Skip to content

Commit 15b26ef

Browse files
snowie2000zdz
authored andcommitted
Add a retry mechanism for domain name resolution
1 parent b87dc56 commit 15b26ef

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

client/src/main.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::thread;
1313
use std::time::Duration;
1414
use std::time::{SystemTime, UNIX_EPOCH};
1515
use tokio::time;
16+
use std::thread::sleep;
1617

1718
use stat_common::server_status::{IpInfo, StatRequest, SysInfo};
1819
type GenericError = Box<dyn std::error::Error + Send + Sync>;
@@ -223,7 +224,22 @@ fn http_report(args: &Args, stat_base: &mut StatRequest) -> Result<()> {
223224
domain = format!("{domain}:80");
224225
}
225226
}
226-
let tcp_addr = domain.to_socket_addrs()?.next().unwrap();
227+
let mut retries = 0;
228+
let max_retries = 10; // Indicates a maximum of 1024s delay = 17 minutes
229+
let tcp_addr = loop {
230+
match domain.to_socket_addrs() {
231+
Ok(mut addrs) => break addrs.next().unwrap(),
232+
Err(e) => {
233+
if retries >= max_retries {
234+
return Err(Box::new(e)); // Return the error if retries are exhausted
235+
}
236+
retries += 1;
237+
let delay = Duration::from_secs(2u64.pow(retries)); // Exponential backoff
238+
eprintln!("Name resolution failed, retrying in {:?}...", delay);
239+
sleep(delay);
240+
}
241+
}
242+
};
227243
let (ipv4, ipv6) = (tcp_addr.is_ipv4(), tcp_addr.is_ipv6());
228244
if ipv4 {
229245
stat_base.online4 = ipv4;

0 commit comments

Comments
 (0)