aboutsummaryrefslogtreecommitdiff
path: root/p2p/examples/peer.rs
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/examples/peer.rs
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/examples/peer.rs')
-rw-r--r--p2p/examples/peer.rs43
1 files changed, 20 insertions, 23 deletions
diff --git a/p2p/examples/peer.rs b/p2p/examples/peer.rs
index f805d68..5ff365d 100644
--- a/p2p/examples/peer.rs
+++ b/p2p/examples/peer.rs
@@ -1,13 +1,16 @@
+mod shared;
+
use std::sync::Arc;
use clap::Parser;
-use easy_parallel::Parallel;
-use smol::{channel, future, Executor};
+use smol::{channel, Executor};
use karyons_net::{Endpoint, Port};
use karyons_p2p::{Backend, Config, PeerID};
+use shared::run_executor;
+
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
@@ -50,33 +53,27 @@ fn main() {
..Default::default()
};
+ // Create a new Executor
+ let ex = Arc::new(Executor::new());
+
// Create a new Backend
- let backend = Backend::new(peer_id, config);
+ let backend = Backend::new(peer_id, config, ex.clone());
let (ctrlc_s, ctrlc_r) = channel::unbounded();
let handle = move || ctrlc_s.try_send(()).unwrap();
ctrlc::set_handler(handle).unwrap();
- let (signal, shutdown) = channel::unbounded::<()>();
-
- // Create a new Executor
- let ex = Arc::new(Executor::new());
-
- let task = async {
- // Run the backend
- backend.run(ex.clone()).await.unwrap();
+ run_executor(
+ async {
+ // Run the backend
+ backend.run().await.unwrap();
- // Wait for ctrlc signal
- ctrlc_r.recv().await.unwrap();
-
- // Shutdown the backend
- backend.shutdown().await;
-
- drop(signal);
- };
+ // Wait for ctrlc signal
+ ctrlc_r.recv().await.unwrap();
- // Run four executor threads.
- Parallel::new()
- .each(0..4, |_| future::block_on(ex.run(shutdown.recv())))
- .finish(|| future::block_on(task));
+ // Shutdown the backend
+ backend.shutdown().await;
+ },
+ ex,
+ );
}