Skip to content

Commit

Permalink
Make block_on_dangerous respect exit, metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 14, 2022
1 parent b2192db commit 13e71b3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion common/task_executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Sigma Prime <contact@sigmaprime.io>"]
edition = "2021"

[dependencies]
tokio = { version = "1.14.0", features = ["rt-multi-thread"] }
tokio = { version = "1.14.0", features = ["rt-multi-thread", "macros"] }
slog = "2.5.2"
futures = "0.3.7"
exit-future = "0.2.0"
Expand Down
38 changes: 35 additions & 3 deletions common/task_executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,44 @@ impl TaskExecutor {
pub fn block_on_dangerous<F: Future>(
&self,
future: F,
_name: &'static str,
name: &'static str,
) -> Option<F::Output>
where {
let timer = metrics::start_timer_vec(&metrics::BLOCK_ON_TASKS_HISTOGRAM, &[name]);
metrics::inc_gauge_vec(&metrics::BLOCK_ON_TASKS_COUNT, &[name]);
let log = self.log.clone();
let handle = self.handle()?;
// TODO(paul): respect the shutdown signal and the name.
Some(handle.block_on(future))
let exit = self.exit.clone();

debug!(
log,
"Starting block_on task";
"name" => name
);

handle.block_on(async {
let output = tokio::select! {
output = future => {
debug!(
log,
"Completed block_on task";
"name" => name
);
Some(output)
},
_ = exit => {
debug!(
log,
"Cancelled block_on task";
"name" => name,
);
None
}
};
metrics::dec_gauge_vec(&metrics::BLOCK_ON_TASKS_COUNT, &[name]);
drop(timer);
output
})
}

/// Returns a `Handle` to the current runtime.
Expand Down
10 changes: 10 additions & 0 deletions common/task_executor/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ lazy_static! {
"Time taken by blocking tasks",
&["blocking_task_hist"]
);
pub static ref BLOCK_ON_TASKS_COUNT: Result<IntGaugeVec> = try_create_int_gauge_vec(
"block_on_tasks_count",
"Total number of block_on_dangers tasks spawned",
&["name"]
);
pub static ref BLOCK_ON_TASKS_HISTOGRAM: Result<HistogramVec> = try_create_histogram_vec(
"block_on_tasks_histogram",
"Time taken by block_on_dangerous tasks",
&["name"]
);
pub static ref TASKS_HISTOGRAM: Result<HistogramVec> = try_create_histogram_vec(
"async_tasks_time_histogram",
"Time taken by async tasks",
Expand Down

0 comments on commit 13e71b3

Please sign in to comment.