Skip to content

Commit 6c8a4bc

Browse files
Copilotevilsocket
andauthored
fix: address code review - Option-typed last_err, document CONNECT_TIMEOUT value
Agent-Logs-Url: https://github.com/evilsocket/cake/sessions/272a2117-aebc-4a10-a2ab-0348f3040921 Co-authored-by: evilsocket <86922+evilsocket@users.noreply.github.com>
1 parent 8881bc5 commit 6c8a4bc

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

  • cake-core/src/cake/sharding

cake-core/src/cake/sharding/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,11 @@ pub async fn master_setup(
385385
// Retry up to 3 times with exponential backoff (1 s, 2 s, 4 s).
386386
// iOS workers may need a brief moment for the TCP listener to become
387387
// fully reachable after the UDP discovery advertisement is sent.
388+
// 10 s per attempt is enough for a LAN connection while still failing
389+
// fast enough to give a useful error within ~30 s overall.
388390
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
389391
const MAX_ATTEMPTS: u32 = 3;
390-
let mut last_err = anyhow!("no attempt made");
392+
let mut last_err: Option<anyhow::Error> = None;
391393
let mut stream = None;
392394
for attempt in 0..MAX_ATTEMPTS {
393395
if attempt > 0 {
@@ -412,19 +414,21 @@ pub async fn master_setup(
412414
"connect attempt {}/{} to '{}' at {} failed: {}",
413415
attempt + 1, MAX_ATTEMPTS, &worker.name, &worker.host, e
414416
);
415-
last_err = anyhow!("can't connect to {}: {}", &worker.host, e);
417+
last_err = Some(anyhow!("can't connect to {}: {}", &worker.host, e));
416418
}
417419
Err(_) => {
418420
log::warn!(
419421
"connect attempt {}/{} to '{}' at {} timed out after {:.0}s",
420422
attempt + 1, MAX_ATTEMPTS, &worker.name, &worker.host,
421423
CONNECT_TIMEOUT.as_secs_f32(),
422424
);
423-
last_err = anyhow!("can't connect to {}: connection timed out", &worker.host);
425+
last_err = Some(anyhow!("can't connect to {}: connection timed out", &worker.host));
424426
}
425427
}
426428
}
427-
let mut stream = stream.ok_or(last_err)?;
429+
let mut stream = stream.ok_or_else(|| {
430+
last_err.unwrap_or_else(|| anyhow!("can't connect to {}: all attempts failed", &worker.host))
431+
})?;
428432
let _ = stream.set_nodelay(true);
429433

430434
// Mutual authentication

0 commit comments

Comments
 (0)