Skip to content

Commit

Permalink
make name an option
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Aug 1, 2023
1 parent 632782b commit 5885ba6
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions crates/tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,35 @@ impl TaskExecutor {
///
/// With `tokio-console` feature flag enabled, the tasks are spawned through an unstable task
/// builder. The returned `Result` is currently unreachable. Ref: <https://docs.rs/tokio/latest/src/tokio/task/builder.rs.html#91>.
fn spawn_on_rt<F>(&self, name: &'static str, fut: F, task_kind: TaskKind) -> JoinHandle<()>
fn spawn_on_rt<F>(
&self,
fut: F,
task_kind: TaskKind,
name: Option<&'static str>,
) -> JoinHandle<()>
where
F: Future<Output = ()> + Send + 'static,
{
match task_kind {
TaskKind::Default => {
if cfg!(feature = "tokio-console") {
tokio::task::Builder::new()
.name(name)
.spawn_on(fut, &self.handle)
.expect("noop")
let mut builder = tokio::task::Builder::new();
if let Some(name) = name {
builder = builder.name(name);
}
builder.spawn_on(fut, &self.handle).expect("noop")
} else {
self.handle.spawn(fut)
}
}
TaskKind::Blocking => {
if cfg!(feature = "tokio-console") {
let handle = self.handle.clone();
tokio::task::Builder::new()
.name(name)
let mut builder = tokio::task::Builder::new();
if let Some(name) = name {
builder = builder.name(name);
}
builder
.spawn_blocking_on(move || handle.block_on(fut), &self.handle)
.expect("noop")
} else {
Expand All @@ -297,7 +306,7 @@ impl TaskExecutor {
}
.in_current_span();

self.spawn_on_rt("", task, task_kind)
self.spawn_on_rt(task, task_kind, None)
}

/// Spawns the task onto the runtime.
Expand Down Expand Up @@ -366,7 +375,7 @@ impl TaskExecutor {
let _ = select(on_shutdown, task).await;
};

self.spawn_on_rt(name, task, task_kind)
self.spawn_on_rt(task, task_kind, Some(name))
}

/// This spawns a critical blocking task onto the runtime.
Expand Down

0 comments on commit 5885ba6

Please sign in to comment.