tests_fuzz/utils/
health.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::time::Duration;
16
17use crate::utils::info;
18
19/// Check health of the processing.
20#[async_trait::async_trait]
21pub trait HealthChecker: Send + Sync {
22    async fn check(&self);
23
24    fn wait_timeout(&self) -> Duration;
25}
26
27/// Http health checker.
28pub struct HttpHealthChecker {
29    pub url: String,
30}
31
32#[async_trait::async_trait]
33impl HealthChecker for HttpHealthChecker {
34    async fn check(&self) {
35        loop {
36            match reqwest::get(&self.url).await {
37                Ok(resp) => {
38                    if resp.status() == 200 {
39                        info!("Health checked!");
40                        return;
41                    }
42                    info!("Failed to check health, status: {}", resp.status());
43                }
44                Err(err) => {
45                    info!("Failed to check health, error: {err:?}");
46                }
47            }
48
49            info!("Checking health later...");
50            tokio::time::sleep(Duration::from_secs(1)).await;
51        }
52    }
53
54    fn wait_timeout(&self) -> Duration {
55        Duration::from_secs(5)
56    }
57}