From cc1d61c401e52ba3b6cd264c5400fb7ab52522dc Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 22 May 2024 18:23:14 +0200 Subject: p2p: add enable_monitor field to Config --- p2p/examples/monitor.rs | 1 + p2p/src/backend.rs | 2 +- p2p/src/config.rs | 5 +++++ p2p/src/monitor.rs | 15 ++++++++++----- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/p2p/examples/monitor.rs b/p2p/examples/monitor.rs index 7b9e5d2..32c8959 100644 --- a/p2p/examples/monitor.rs +++ b/p2p/examples/monitor.rs @@ -45,6 +45,7 @@ fn main() { peer_endpoints: cli.peer_endpoints, bootstrap_peers: cli.bootstrap_peers, discovery_port: cli.discovery_port.unwrap_or(0), + enable_monitor: true, ..Default::default() }; diff --git a/p2p/src/backend.rs b/p2p/src/backend.rs index c05a693..0db1cee 100644 --- a/p2p/src/backend.rs +++ b/p2p/src/backend.rs @@ -39,7 +39,7 @@ impl Backend { /// Creates a new Backend. pub fn new(key_pair: &KeyPair, config: Config, ex: Executor) -> ArcBackend { let config = Arc::new(config); - let monitor = Arc::new(Monitor::new()); + let monitor = Arc::new(Monitor::new(config.clone())); let conn_queue = ConnQueue::new(); let peer_id = PeerID::try_from(key_pair.public()) diff --git a/p2p/src/config.rs b/p2p/src/config.rs index fffbebd..4f6554d 100644 --- a/p2p/src/config.rs +++ b/p2p/src/config.rs @@ -7,6 +7,9 @@ pub struct Config { /// Represents the network version. pub version: Version, + /// Enable monitor + pub enable_monitor: bool, + ///////////////// // PeerPool //////////////// @@ -81,6 +84,8 @@ impl Default for Config { Config { version: "0.1.0".parse().unwrap(), + enable_monitor: false, + handshake_timeout: 2, ping_interval: 20, ping_timeout: 2, diff --git a/p2p/src/monitor.rs b/p2p/src/monitor.rs index bc3ea7f..945c6aa 100644 --- a/p2p/src/monitor.rs +++ b/p2p/src/monitor.rs @@ -1,11 +1,11 @@ -use std::fmt; - -use crate::PeerID; +use std::{fmt, sync::Arc}; use karyon_core::event::{ArcEventSys, EventListener, EventSys, EventValue, EventValueTopic}; use karyon_net::Endpoint; +use crate::{Config, PeerID}; + /// Responsible for network and system monitoring. /// /// It use pub-sub pattern to notify the subscribers with new events. @@ -37,13 +37,16 @@ use karyon_net::Endpoint; /// ``` pub struct Monitor { event_sys: ArcEventSys, + + config: Arc, } impl Monitor { /// Creates a new Monitor - pub(crate) fn new() -> Self { + pub(crate) fn new(config: Arc) -> Self { Self { event_sys: EventSys::new(), + config, } } @@ -52,7 +55,9 @@ impl Monitor { where E: EventValue + Clone + EventValueTopic, { - self.event_sys.emit(&event).await + if self.config.enable_monitor { + self.event_sys.emit(&event).await + } } /// Registers a new event listener for connection events. -- cgit v1.2.3