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/protocols/ping.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'p2p/src/protocols') diff --git a/p2p/src/protocols/ping.rs b/p2p/src/protocols/ping.rs index b337494..dc1b9a1 100644 --- a/p2p/src/protocols/ping.rs +++ b/p2p/src/protocols/ping.rs @@ -39,7 +39,6 @@ pub struct PingProtocol { peer: ArcPeer, ping_interval: u64, ping_timeout: u64, - task_group: TaskGroup, } impl PingProtocol { @@ -51,7 +50,6 @@ impl PingProtocol { peer, ping_interval, ping_timeout, - task_group: TaskGroup::new(), }) } @@ -130,12 +128,14 @@ impl PingProtocol { impl Protocol for PingProtocol { async fn start(self: Arc, ex: Executor<'_>) -> Result<()> { trace!("Start Ping protocol"); + + let task_group = TaskGroup::new(ex); + let (pong_chan, pong_chan_recv) = channel::bounded(1); let (stop_signal_s, stop_signal) = channel::bounded::>(1); let selfc = self.clone(); - self.task_group.spawn( - ex.clone(), + task_group.spawn( selfc.clone().ping_loop(pong_chan_recv.clone()), |res| async move { if let TaskResult::Completed(result) = res { @@ -148,7 +148,7 @@ impl Protocol for PingProtocol { let result = select(self.recv_loop(&listener, pong_chan), stop_signal.recv()).await; listener.cancel().await; - self.task_group.cancel().await; + task_group.cancel().await; match result { Either::Left(res) => { -- cgit v1.2.3