From 5baf14594331b1b0b60d655240eb398bcce61f7c Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 21 Mar 2024 12:28:47 +0100 Subject: core: minor modification to TaskGroup api --- Cargo.toml | 4 +++- core/src/async_util/task_group.rs | 26 ++++++++++++++++---------- jsonrpc/src/server.rs | 2 +- p2p/src/connector.rs | 2 +- p2p/src/discovery/mod.rs | 2 +- p2p/src/discovery/refresh.rs | 2 +- p2p/src/listener.rs | 2 +- p2p/src/peer/mod.rs | 2 +- p2p/src/peer_pool.rs | 2 +- p2p/src/protocols/ping.rs | 2 +- 10 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b0515fe..ca73eeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,13 @@ [workspace] +resolver = "2" + +# Please ensure that each crate comes before any other crate that depends on it members = [ "core", "net", "p2p", "jsonrpc", ] -resolver = "2" [workspace.package] version = "0.1.0" diff --git a/core/src/async_util/task_group.rs b/core/src/async_util/task_group.rs index 0fb4855..3de2735 100644 --- a/core/src/async_util/task_group.rs +++ b/core/src/async_util/task_group.rs @@ -17,7 +17,7 @@ use super::{executor::global_executor, select, CondWait, Either, Executor}; /// async { /// /// let ex = Arc::new(smol::Executor::new()); -/// let group = TaskGroup::new(ex); +/// let group = TaskGroup::with_executor(ex); /// /// group.spawn(smol::Timer::never(), |_| async {}); /// @@ -34,10 +34,10 @@ pub struct TaskGroup<'a> { } impl TaskGroup<'static> { - /// Creates a new task group without providing an executor + /// Creates a new TaskGroup without providing an executor /// /// This will Spawn a task onto a global executor (single-threaded by default). - pub fn new_without_executor() -> Self { + pub fn new() -> Self { Self { tasks: Mutex::new(Vec::new()), stop_signal: Arc::new(CondWait::new()), @@ -47,8 +47,8 @@ impl TaskGroup<'static> { } impl<'a> TaskGroup<'a> { - /// Creates a new task group - pub fn new(executor: Executor<'a>) -> Self { + /// Creates a new TaskGroup by providing an executor + pub fn with_executor(executor: Executor<'a>) -> Self { Self { tasks: Mutex::new(Vec::new()), stop_signal: Arc::new(CondWait::new()), @@ -75,7 +75,7 @@ impl<'a> TaskGroup<'a> { self.tasks.lock().unwrap().push(task); } - /// Checks if the task group is empty. + /// Checks if the TaskGroup is empty. pub fn is_empty(&self) -> bool { self.tasks.lock().unwrap().is_empty() } @@ -100,6 +100,12 @@ impl<'a> TaskGroup<'a> { } } +impl Default for TaskGroup<'static> { + fn default() -> Self { + Self::new() + } +} + /// The result of a spawned task. #[derive(Debug)] pub enum TaskResult { @@ -172,10 +178,10 @@ mod tests { use std::{future, sync::Arc}; #[test] - fn test_task_group() { + fn test_task_group_with_executor() { let ex = Arc::new(smol::Executor::new()); smol::block_on(ex.clone().run(async move { - let group = Arc::new(TaskGroup::new(ex)); + let group = Arc::new(TaskGroup::with_executor(ex)); group.spawn(future::ready(0), |res| async move { assert!(matches!(res, TaskResult::Completed(0))); @@ -204,9 +210,9 @@ mod tests { } #[test] - fn test_task_group_without_executor() { + fn test_task_group() { smol::block_on(async { - let group = Arc::new(TaskGroup::new_without_executor()); + let group = Arc::new(TaskGroup::new()); group.spawn(future::ready(0), |res| async move { assert!(matches!(res, TaskResult::Completed(0))); diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs index ac1673d..26d632a 100644 --- a/jsonrpc/src/server.rs +++ b/jsonrpc/src/server.rs @@ -54,7 +54,7 @@ impl<'a> Server<'a> { Arc::new(Self { listener: listener.to_listener(), services: RwLock::new(HashMap::new()), - task_group: TaskGroup::new(ex), + task_group: TaskGroup::with_executor(ex), config, }) } diff --git a/p2p/src/connector.rs b/p2p/src/connector.rs index 9bf63f9..de9e746 100644 --- a/p2p/src/connector.rs +++ b/p2p/src/connector.rs @@ -52,7 +52,7 @@ impl Connector { Arc::new(Self { key_pair: key_pair.clone(), max_retries, - task_group: TaskGroup::new(ex), + task_group: TaskGroup::with_executor(ex), monitor, connection_slots, enable_tls, diff --git a/p2p/src/discovery/mod.rs b/p2p/src/discovery/mod.rs index 4b54233..3e437aa 100644 --- a/p2p/src/discovery/mod.rs +++ b/p2p/src/discovery/mod.rs @@ -116,7 +116,7 @@ impl Discovery { outbound_slots, connector, listener, - task_group: TaskGroup::new(ex), + task_group: TaskGroup::with_executor(ex), config, }) } diff --git a/p2p/src/discovery/refresh.rs b/p2p/src/discovery/refresh.rs index e56f0eb..035a581 100644 --- a/p2p/src/discovery/refresh.rs +++ b/p2p/src/discovery/refresh.rs @@ -70,7 +70,7 @@ impl RefreshService { Self { table, listen_endpoint, - task_group: TaskGroup::new(executor.clone()), + task_group: TaskGroup::with_executor(executor.clone()), executor, config, monitor, diff --git a/p2p/src/listener.rs b/p2p/src/listener.rs index c9b7390..4a41482 100644 --- a/p2p/src/listener.rs +++ b/p2p/src/listener.rs @@ -46,7 +46,7 @@ impl Listener { Arc::new(Self { key_pair: key_pair.clone(), connection_slots, - task_group: TaskGroup::new(ex), + task_group: TaskGroup::with_executor(ex), enable_tls, monitor, }) diff --git a/p2p/src/peer/mod.rs b/p2p/src/peer/mod.rs index 1fc5ccf..ca68530 100644 --- a/p2p/src/peer/mod.rs +++ b/p2p/src/peer/mod.rs @@ -76,7 +76,7 @@ impl Peer { remote_endpoint, conn_direction, protocol_events: EventSys::new(), - task_group: TaskGroup::new(ex), + task_group: TaskGroup::with_executor(ex), stop_chan: channel::bounded(1), }) } diff --git a/p2p/src/peer_pool.rs b/p2p/src/peer_pool.rs index 48499fe..4e20c99 100644 --- a/p2p/src/peer_pool.rs +++ b/p2p/src/peer_pool.rs @@ -80,7 +80,7 @@ impl PeerPool { peers: Mutex::new(HashMap::new()), protocols, protocol_versions, - task_group: TaskGroup::new(executor.clone()), + task_group: TaskGroup::with_executor(executor.clone()), executor, monitor, config, diff --git a/p2p/src/protocols/ping.rs b/p2p/src/protocols/ping.rs index 4885c1e..f04e059 100644 --- a/p2p/src/protocols/ping.rs +++ b/p2p/src/protocols/ping.rs @@ -50,7 +50,7 @@ impl PingProtocol { peer, ping_interval, ping_timeout, - task_group: TaskGroup::new(executor), + task_group: TaskGroup::with_executor(executor), }) } -- cgit v1.2.3