From 78884caca030104557ca277dd3a41cefb70f5be8 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 15 Nov 2023 17:16:39 +0300 Subject: improve the TaskGroup API the TaskGroup now holds an Executor instead of passing it when calling its spawn method also, define a global executor `Executor<'static>` and use static lifetime instead of a lifetime placeholder This improvement simplify the code for spawning a new task. There is no need to pass the executor around. --- p2p/src/backend.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'p2p/src/backend.rs') diff --git a/p2p/src/backend.rs b/p2p/src/backend.rs index bb18f06..bb0d891 100644 --- a/p2p/src/backend.rs +++ b/p2p/src/backend.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use log::info; -use karyons_core::{pubsub::Subscription, Executor}; +use karyons_core::{pubsub::Subscription, GlobalExecutor}; use crate::{ config::Config, @@ -34,15 +34,16 @@ pub type ArcBackend = Arc; /// // Create the configuration for the backend. /// let mut config = Config::default(); /// -/// // Create a new Backend -/// let backend = Backend::new(peer_id, config); /// /// // Create a new Executor /// let ex = Arc::new(Executor::new()); /// +/// // Create a new Backend +/// let backend = Backend::new(peer_id, config, ex.clone()); +/// /// let task = async { /// // Run the backend -/// backend.run(ex.clone()).await.unwrap(); +/// backend.run().await.unwrap(); /// /// // .... /// @@ -72,14 +73,14 @@ pub struct Backend { impl Backend { /// Creates a new Backend. - pub fn new(id: PeerID, config: Config) -> ArcBackend { + pub fn new(id: PeerID, config: Config, ex: GlobalExecutor) -> ArcBackend { let config = Arc::new(config); let monitor = Arc::new(Monitor::new()); + let cq = ConnQueue::new(); - let conn_queue = ConnQueue::new(); + let peer_pool = PeerPool::new(&id, cq.clone(), config.clone(), monitor.clone(), ex.clone()); - let peer_pool = PeerPool::new(&id, conn_queue.clone(), config.clone(), monitor.clone()); - let discovery = Discovery::new(&id, conn_queue, config.clone(), monitor.clone()); + let discovery = Discovery::new(&id, cq, config.clone(), monitor.clone(), ex); Arc::new(Self { id: id.clone(), @@ -91,10 +92,10 @@ impl Backend { } /// Run the Backend, starting the PeerPool and Discovery instances. - pub async fn run(self: &Arc, ex: Executor<'_>) -> Result<()> { + pub async fn run(self: &Arc) -> Result<()> { info!("Run the backend {}", self.id); - self.peer_pool.start(ex.clone()).await?; - self.discovery.start(ex.clone()).await?; + self.peer_pool.start().await?; + self.discovery.start().await?; Ok(()) } -- cgit v1.2.3