Skip to content

Commit

Permalink
Add comments to TaskSpawner
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 9d73de2 commit 343aa37
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions beacon_node/network/src/beacon_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,13 +1607,21 @@ impl<T: BeaconChainTypes> BeaconProcessor<T> {
}
}

pub struct TaskSpawner {
/// Spawns tasks that are either:
///
/// - Blocking (i.e. intensive methods that shouldn't run on the core `tokio` executor)
/// - Async (i.e. `async` methods)
///
/// Takes a `SendOnDrop` and ensures it is dropped after the task completes. This frees the beacon
/// processor worker so a new task can be started.
struct TaskSpawner {
executor: TaskExecutor,
send_idle_on_drop: SendOnDrop,
}

impl TaskSpawner {
pub fn spawn_async(self, task: impl Future<Output = ()> + Send + 'static) {
/// Spawn an async task, dropping the `SendOnDrop` after the task has completed.
fn spawn_async(self, task: impl Future<Output = ()> + Send + 'static) {
self.executor.spawn(
async {
task.await;
Expand All @@ -1623,7 +1631,8 @@ impl TaskSpawner {
)
}

pub fn spawn_blocking<F>(self, task: F)
/// Spawn a blocking task, dropping the `SendOnDrop` after the task has completed.
fn spawn_blocking<F>(self, task: F)
where
F: FnOnce() + Send + 'static,
{
Expand All @@ -1636,6 +1645,11 @@ impl TaskSpawner {
)
}

/// Spawn a blocking task, passing the `SendOnDrop` into the task.
///
/// ## Notes
///
/// Users must ensure the `SendOnDrop` is dropped at the appropriate time!
pub fn spawn_blocking_with_manual_send_idle<F>(self, task: F)
where
F: FnOnce(SendOnDrop) + Send + 'static,
Expand Down

0 comments on commit 343aa37

Please sign in to comment.