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/connector.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'p2p/src/connector.rs') diff --git a/p2p/src/connector.rs b/p2p/src/connector.rs index 3932c41..f41ab57 100644 --- a/p2p/src/connector.rs +++ b/p2p/src/connector.rs @@ -4,7 +4,7 @@ use log::{trace, warn}; use karyons_core::{ async_utils::{Backoff, TaskGroup, TaskResult}, - Executor, + GlobalExecutor, }; use karyons_net::{dial, Conn, Endpoint, NetError}; @@ -17,7 +17,7 @@ use crate::{ /// Responsible for creating outbound connections with other peers. pub struct Connector { /// Managing spawned tasks. - task_group: TaskGroup, + task_group: TaskGroup<'static>, /// Manages available outbound slots. connection_slots: Arc, @@ -36,9 +36,10 @@ impl Connector { max_retries: usize, connection_slots: Arc, monitor: Arc, + ex: GlobalExecutor, ) -> Arc { Arc::new(Self { - task_group: TaskGroup::new(), + task_group: TaskGroup::new(ex), monitor, connection_slots, max_retries, @@ -92,14 +93,13 @@ impl Connector { /// Establish a connection to the given `endpoint`. For each new connection, /// it invokes the provided `callback`, and pass the connection to the callback. - pub async fn connect_with_cback<'a, Fut>( + pub async fn connect_with_cback( self: &Arc, - ex: Executor<'a>, endpoint: &Endpoint, - callback: impl FnOnce(Conn) -> Fut + Send + 'a, + callback: impl FnOnce(Conn) -> Fut + Send + 'static, ) -> Result<()> where - Fut: Future> + Send + 'a, + Fut: Future> + Send + 'static, { let conn = self.connect(endpoint).await?; @@ -116,8 +116,7 @@ impl Connector { selfc.connection_slots.remove().await; }; - self.task_group - .spawn(ex.clone(), callback(conn), on_disconnect); + self.task_group.spawn(callback(conn), on_disconnect); Ok(()) } -- cgit v1.2.3