aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-22 18:23:14 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-22 18:23:14 +0200
commitcc1d61c401e52ba3b6cd264c5400fb7ab52522dc (patch)
treef427aca981dbc9e526d4dec54971b44016b542cd
parent5df6812dd2254b871eb773dc626b89646ca87495 (diff)
p2p: add enable_monitor field to Config
-rw-r--r--p2p/examples/monitor.rs1
-rw-r--r--p2p/src/backend.rs2
-rw-r--r--p2p/src/config.rs5
-rw-r--r--p2p/src/monitor.rs15
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<MonitorTopic>,
+
+ config: Arc<Config>,
}
impl Monitor {
/// Creates a new Monitor
- pub(crate) fn new() -> Self {
+ pub(crate) fn new(config: Arc<Config>) -> Self {
Self {
event_sys: EventSys::new(),
+ config,
}
}
@@ -52,7 +55,9 @@ impl Monitor {
where
E: EventValue + Clone + EventValueTopic<Topic = MonitorTopic>,
{
- 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.