1use std::{io, path::PathBuf, time::Duration};
6
7use mio::net::UnixStream;
8use tokio::time::sleep;
9use tracing::{debug, trace};
10
11#[derive(thiserror::Error, Debug)]
15pub enum Error {
16 #[error("failed to wait for the socket to be ready, {0}")]
17 Wait(io::Error),
18}
19
20#[tracing::instrument]
24pub async fn connect(path: &PathBuf) -> Result<UnixStream, Error> {
25 loop {
26 return match UnixStream::connect(path) {
27 Ok(stream) => {
28 debug!(
29 path = path.display().to_string(),
30 "Successfully connected to socket"
31 );
32 Ok(stream)
33 }
34 Err(err)
35 if matches!(
36 err.kind(),
37 io::ErrorKind::NotFound | io::ErrorKind::ConnectionRefused
38 ) =>
39 {
40 trace!(
41 path = path.display().to_string(),
42 "Try to connect to socket"
43 );
44 sleep(Duration::from_millis(100)).await;
45 continue;
46 }
47 Err(err) => Err(Error::Wait(err)),
48 };
49 }
50}