Wait for the daemon socket to become available, with exponential backoff.
()
| 59 | |
| 60 | /// Wait for the daemon socket to become available, with exponential backoff. |
| 61 | pub async fn wait_for_daemon() -> Result<()> { |
| 62 | let deadline = tokio::time::Instant::now() + daemon_wait_timeout(); |
| 63 | let mut delay = Duration::from_millis(50); |
| 64 | loop { |
| 65 | if tokio::time::Instant::now() > deadline { |
| 66 | bail!( |
| 67 | "Daemon failed to start within {} seconds", |
| 68 | daemon_wait_timeout().as_secs() |
| 69 | ); |
| 70 | } |
| 71 | if connect_daemon().await.is_ok() { |
| 72 | return Ok(()); |
| 73 | } |
| 74 | // Simple jitter based on current time subseconds |
| 75 | let jitter = SystemTime::now() |
| 76 | .duration_since(SystemTime::UNIX_EPOCH) |
| 77 | .map(|d| { |
| 78 | Duration::from_millis(d.subsec_nanos() as u64 % (delay.as_millis() as u64 + 1)) |
| 79 | }) |
| 80 | .unwrap_or_default(); |
| 81 | tokio::time::sleep(delay + jitter).await; |
| 82 | delay = (delay * 2).min(Duration::from_millis(500)); |
| 83 | } |
| 84 | } |
no test coverage detected