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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    application::storage::PeerMetadataStorage,
    protocols::health_checker::{
        interface::HealthCheckNetworkInterface, HealthChecker, HealthCheckerNetworkEvents,
        HealthCheckerNetworkSender,
    },
};
use diem_config::network_id::NetworkContext;
use diem_time_service::TimeService;
use std::{sync::Arc, time::Duration};
use tokio::runtime::Handle;

pub struct HealthCheckerBuilder {
    service: Option<HealthChecker>,
}

impl HealthCheckerBuilder {
    pub fn new(
        network_context: Arc<NetworkContext>,
        time_service: TimeService,
        ping_interval_ms: u64,
        ping_timeout_ms: u64,
        ping_failures_tolerated: u64,
        network_tx: HealthCheckerNetworkSender,
        network_rx: HealthCheckerNetworkEvents,
        peer_metadata_storage: Arc<PeerMetadataStorage>,
    ) -> Self {
        let service = HealthChecker::new(
            network_context,
            time_service,
            HealthCheckNetworkInterface::new(peer_metadata_storage, network_tx, network_rx),
            Duration::from_millis(ping_interval_ms),
            Duration::from_millis(ping_timeout_ms),
            ping_failures_tolerated,
        );
        Self {
            service: Some(service),
        }
    }

    pub fn start(&mut self, executor: &Handle) {
        if let Some(service) = self.service.take() {
            executor.spawn(service.start());
        }
    }
}