diff options
| author | hozan23 <hozan23@proton.me> | 2023-11-15 17:16:39 +0300 | 
|---|---|---|
| committer | hozan23 <hozan23@proton.me> | 2023-11-15 17:16:39 +0300 | 
| commit | 78884caca030104557ca277dd3a41cefb70f5be8 (patch) | |
| tree | c33650dfe44a219e395dff1966d298b58b09acb3 /p2p/src/backend.rs | |
| parent | f0729022589ee8e48b5558ab30462f95d06fe6df (diff) | |
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.
Diffstat (limited to 'p2p/src/backend.rs')
| -rw-r--r-- | p2p/src/backend.rs | 23 | 
1 files changed, 12 insertions, 11 deletions
| 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<Backend>;  /// // 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<Self>, ex: Executor<'_>) -> Result<()> { +    pub async fn run(self: &Arc<Self>) -> 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(())      } | 
