mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
Extend Task Spawner with spawn blocking function (#1908)
This commit is contained in:
@ -73,6 +73,9 @@ pub trait TaskSpawner: Send + Sync + Unpin + std::fmt::Debug + DynClone {
|
||||
|
||||
/// This spawns a critical task onto the runtime.
|
||||
fn spawn_critical(&self, name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()>;
|
||||
|
||||
/// Spawns a blocking task onto the runtime.
|
||||
fn spawn_blocking(&self, name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()>;
|
||||
}
|
||||
|
||||
dyn_clone::clone_trait_object!(TaskSpawner);
|
||||
@ -90,6 +93,10 @@ impl TaskSpawner for TokioTaskExecutor {
|
||||
fn spawn_critical(&self, _name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()> {
|
||||
tokio::task::spawn(fut)
|
||||
}
|
||||
|
||||
fn spawn_blocking(&self, _name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()> {
|
||||
tokio::task::spawn_blocking(move || tokio::runtime::Handle::current().block_on(fut))
|
||||
}
|
||||
}
|
||||
|
||||
/// Many reth components require to spawn tasks for long-running jobs. For example `discovery`
|
||||
@ -342,6 +349,10 @@ impl TaskSpawner for TaskExecutor {
|
||||
fn spawn_critical(&self, name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()> {
|
||||
TaskExecutor::spawn_critical(self, name, fut)
|
||||
}
|
||||
|
||||
fn spawn_blocking(&self, _name: &'static str, fut: BoxFuture<'static, ()>) -> JoinHandle<()> {
|
||||
self.spawn_blocking(fut)
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines how a task is spawned
|
||||
|
||||
Reference in New Issue
Block a user