1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::{
    error::Error as StdError,
    fmt,
    net::{AddrParseError, SocketAddr},
    time::{Duration, Instant},
};

use async_trait::async_trait;
use hyper::{client::HttpConnector, http::uri::InvalidUri, Body, Client, Request, Response, Uri};
use tokio::{io::AsyncWriteExt, net::TcpStream, time};

use crate::{Dependency, DependencyWaitError};

pub use hyper::Method as HttpMethod;

const ITER_GAP: Duration = Duration::from_millis(250); // ms

/// Error returned from a network [`Dependency::wait`](Dependency::wait) method.
#[derive(thiserror::Error, Debug)]
enum NetServiceWaitError {
    /// Rejected network request.
    #[error("Rejection: {}", .error)]
    Rejection {
        /// Error from the dependency.
        error: Box<dyn StdError + Send + Sync>,
    },
    /// Request timeout.
    #[error("Timeout")]
    Timeout,
}

impl DependencyWaitError for NetServiceWaitError {}

/// TCP service.
pub struct TcpService {
    /// A tag used as an identificator of the dependency in the output.
    pub tag: String,
    /// Service address.
    pub addr: SocketAddr,
    /// Service wait timeout.
    pub timeout: Duration,
    /// Optional wait time after a successful response from the TCP service.
    pub warm_up: Option<Duration>,
}

impl TcpService {
    /// Consructs new TcpService.
    pub fn new(
        tag: impl Into<String>,
        host: impl fmt::Display,
        port: impl fmt::Display,
        timeout: Duration,
        warm_up: Option<Duration>,
    ) -> Result<Self, AddrParseError> {
        let addr = format!("{}:{}", host, port).parse()?;

        Ok(Self {
            tag: tag.into(),
            addr,
            timeout,
            warm_up,
        })
    }
}

#[async_trait]
impl Dependency for TcpService {
    fn tag(&self) -> &str {
        &self.tag
    }

    async fn check(&self) -> Result<(), ()> {
        match TcpStream::connect(&self.addr).await {
            Ok(_) => Ok(()),
            Err(_) => Err(()),
        }
    }

    async fn wait(&self) -> Result<(), Box<dyn DependencyWaitError>> {
        let start = Instant::now();

        loop {
            match time::timeout(
                self.timeout - start.elapsed(),
                TcpStream::connect(&self.addr),
            )
            .await
            {
                Ok(Ok(mut stream)) => {
                    if let Err(error) = stream.shutdown().await {
                        eprintln!("Failed to close socket: {}", error);
                    };

                    if let Some(duration) = self.warm_up {
                        time::sleep(duration).await;
                    }

                    return Ok(());
                }
                Ok(Err(_)) => (),
                Err(_) => {
                    return Err(Box::new(NetServiceWaitError::Timeout));
                }
            }

            if start.elapsed() >= self.timeout {
                return Err(Box::new(NetServiceWaitError::Timeout));
            }

            time::sleep(ITER_GAP).await;
        }
    }
}

/// HTTP service.
pub struct HttpService {
    /// A tag used as an identificator of the dependency in the output.
    pub tag: String,
    /// Service address.
    pub addr: Uri,
    /// HTTP method.
    pub method: HttpMethod,
    /// Service wait timeout.
    pub timeout: Duration,
}

impl HttpService {
    fn http_connector() -> HttpConnector {
        HttpConnector::new()
    }

    #[cfg(feature = "tls")]
    fn https_connector() -> tls::HttpsConnector<HttpConnector> {
        tls::HttpsConnector::new()
    }

    #[cfg(not(feature = "tls"))]
    fn https_connector() -> HttpConnector {
        unreachable!("Cannot use https_connector method without tls feature");
    }
}

#[derive(Debug)]
struct HttpError {
    status: hyper::StatusCode,
}

impl std::error::Error for HttpError {}

impl fmt::Display for HttpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.status)
    }
}

impl From<hyper::Response<Body>> for HttpError {
    fn from(res: hyper::Response<Body>) -> Self {
        Self {
            status: res.status(),
        }
    }
}

impl HttpService {
    /// Consructs new HttpService.
    pub fn new(
        tag: impl Into<String>,
        host: impl fmt::Display,
        port: impl fmt::Display,
        path: impl fmt::Display,
        ssl: bool,
        method: HttpMethod,
        timeout: Duration,
    ) -> Result<Self, InvalidUri> {
        let addr = format!(
            "http{}://{}:{}{}",
            if ssl { "s" } else { "" },
            host,
            port,
            path
        )
        .parse()?;

        Ok(Self {
            tag: tag.into(),
            addr,
            method,
            timeout,
        })
    }

    pub(crate) fn build_req(&self) -> Request<Body> {
        Request::builder()
            .method(&self.method)
            .uri(&self.addr)
            .body(Body::default())
            .expect("Failed to build HTTP request")
    }

    fn handle_res(res: Response<Body>) -> Result<(), Box<dyn DependencyWaitError>> {
        if res.status().is_success() {
            Ok(())
        } else {
            Err(Box::new(NetServiceWaitError::Rejection {
                error: Box::new(Into::<HttpError>::into(res)),
            }))
        }
    }
}

#[async_trait]
impl Dependency for HttpService {
    fn tag(&self) -> &str {
        &self.tag
    }

    async fn check(&self) -> Result<(), ()> {
        match self.addr.scheme_str() {
            Some("https") => {
                let connector = Self::https_connector();
                let client = Client::builder().build(connector);
                let req = self.build_req();
                let res = client.request(req).await.map_err(|_| ())?;
                Self::handle_res(res).map_err(|_| ())
            }
            Some(_) | None => {
                let connector = Self::http_connector();
                let client = Client::builder().build(connector);
                let req = self.build_req();
                let res = client.request(req).await.map_err(|_| ())?;
                Self::handle_res(res).map_err(|_| ())
            }
        }
    }

    async fn wait(&self) -> Result<(), Box<dyn DependencyWaitError>> {
        let start = Instant::now();

        match self.addr.scheme_str() {
            Some("https") => {
                let connector = Self::https_connector();
                let client = Client::builder().build(connector);

                loop {
                    let req = self.build_req();

                    match time::timeout(self.timeout - start.elapsed(), client.request(req)).await {
                        Ok(Ok(res)) => return Self::handle_res(res),
                        Ok(Err(_)) => (),
                        Err(_) => return Err(Box::new(NetServiceWaitError::Timeout)),
                    }

                    if start.elapsed() >= self.timeout {
                        return Err(Box::new(NetServiceWaitError::Timeout));
                    }

                    time::sleep(ITER_GAP).await;
                }
            }
            Some(_) | None => {
                let connector = Self::http_connector();
                let client = Client::builder().build(connector);

                loop {
                    let req = self.build_req();

                    match time::timeout(self.timeout - start.elapsed(), client.request(req)).await {
                        Ok(Ok(res)) => return Self::handle_res(res),
                        Ok(Err(_)) => (),
                        Err(_) => return Err(Box::new(NetServiceWaitError::Timeout)),
                    }

                    if start.elapsed() >= self.timeout {
                        return Err(Box::new(NetServiceWaitError::Timeout));
                    }

                    time::sleep(ITER_GAP).await;
                }
            }
        }
    }
}