aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/protocols
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2023-11-15 17:16:39 +0300
committerhozan23 <hozan23@proton.me>2023-11-15 17:16:39 +0300
commit78884caca030104557ca277dd3a41cefb70f5be8 (patch)
treec33650dfe44a219e395dff1966d298b58b09acb3 /p2p/src/protocols
parentf0729022589ee8e48b5558ab30462f95d06fe6df (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/protocols')
-rw-r--r--p2p/src/protocols/ping.rs10
1 files changed, 5 insertions, 5 deletions
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<Self>, 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::<Result<()>>(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) => {