Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions client/service/src/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ mod tests;
/// Default task group name.
pub const DEFAULT_GROUP_NAME: &'static str = "default";

/// The name of a group a task belongs to.
///
/// This name is passed belong-side the task name to the prometheus metrics and can be used
/// to group tasks.
pub enum GroupName {
/// Sets the group name to `default`.
Default,
/// Use the specifically given name as group name.
Specific(&'static str),
}

impl From<Option<&'static str>> for GroupName {
fn from(name: Option<&'static str>) -> Self {
match name {
Some(name) => Self::Specific(name),
None => Self::Default,
}
}
}

impl From<&'static str> for GroupName {
fn from(name: &'static str) -> Self {
Self::Specific(name)
}
}

/// An handle for spawning tasks in the service.
#[derive(Clone)]
pub struct SpawnTaskHandle {
Expand All @@ -51,10 +77,10 @@ pub struct SpawnTaskHandle {
}

impl SpawnTaskHandle {
/// Spawns the given task with the given name and an optional group name.
/// Spawns the given task with the given name and a group name.
/// If group is not specified `DEFAULT_GROUP_NAME` will be used.
///
/// Note that the `name`/`group` is a `&'static str`. The reason for this choice is that
/// Note that the `name` is a `&'static str`. The reason for this choice is that
/// statistics about this task are getting reported to the Prometheus endpoint (if enabled), and
/// that therefore the set of possible task names must be bounded.
///
Expand All @@ -63,7 +89,7 @@ impl SpawnTaskHandle {
pub fn spawn(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
) {
self.spawn_inner(name, group, task, TaskType::Async)
Expand All @@ -73,7 +99,7 @@ impl SpawnTaskHandle {
pub fn spawn_blocking(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
) {
self.spawn_inner(name, group, task, TaskType::Blocking)
Expand All @@ -83,7 +109,7 @@ impl SpawnTaskHandle {
fn spawn_inner(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
task_type: TaskType,
) {
Expand All @@ -94,8 +120,12 @@ impl SpawnTaskHandle {

let on_exit = self.on_exit.clone();
let metrics = self.metrics.clone();
// If no group is specified use default.
let group = group.unwrap_or(DEFAULT_GROUP_NAME);

let group = match group.into() {
GroupName::Specific(var) => var,
// If no group is specified use default.
GroupName::Default => DEFAULT_GROUP_NAME,
};

// Note that we increase the started counter here and not within the future. This way,
// we could properly visualize on Prometheus situations where the spawning doesn't work.
Expand Down Expand Up @@ -198,7 +228,7 @@ impl SpawnEssentialTaskHandle {
pub fn spawn(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
) {
self.spawn_inner(name, group, task, TaskType::Async)
Expand All @@ -210,7 +240,7 @@ impl SpawnEssentialTaskHandle {
pub fn spawn_blocking(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
) {
self.spawn_inner(name, group, task, TaskType::Blocking)
Expand All @@ -219,7 +249,7 @@ impl SpawnEssentialTaskHandle {
fn spawn_inner(
&self,
name: &'static str,
group: Option<&'static str>,
group: impl Into<GroupName>,
task: impl Future<Output = ()> + Send + 'static,
task_type: TaskType,
) {
Expand Down