-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "http" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[features] | ||
default = [] | ||
full = ["metrics"] | ||
metrics = ["dep:metrics", "dep:future"] | ||
|
||
[dependencies] | ||
future = { path = "../future", features = ["metrics"], optional = true } | ||
metrics = { path = "../metrics", optional = true } | ||
hyper = "0.14" | ||
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "time", "macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use { | ||
future::FutureExt, | ||
metrics::TaskMetrics, | ||
std::{future::Future, time::Duration}, | ||
}; | ||
|
||
/// Global `hyper` service task executor that uses the `tokio` runtime and adds | ||
/// metrics for the executed tasks. | ||
#[derive(Default, Clone)] | ||
pub struct ServiceTaskExecutor { | ||
timeout: Option<Duration>, | ||
metrics_name: &'static str, | ||
} | ||
|
||
impl ServiceTaskExecutor { | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Optional `task_name` metrics attribute. | ||
pub fn name(self, metrics_name: Option<&'static str>) -> Self { | ||
Self { | ||
timeout: self.timeout, | ||
metrics_name: metrics_name.unwrap_or(""), | ||
} | ||
} | ||
|
||
/// Apply a timeout to all service tasks to prevent them from becoming | ||
/// zombies for various reasons. | ||
/// | ||
/// Default is no timeout. | ||
pub fn timeout(self, timeout: Option<Duration>) -> Self { | ||
Self { | ||
timeout, | ||
metrics_name: self.metrics_name, | ||
} | ||
} | ||
} | ||
|
||
impl<F> hyper::rt::Executor<F> for ServiceTaskExecutor | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
fn execute(&self, fut: F) { | ||
static METRICS: TaskMetrics = TaskMetrics::new("hyper_service_task"); | ||
|
||
let fut = fut.with_metrics(METRICS.with_name(self.metrics_name)); | ||
let timeout = self.timeout; | ||
|
||
tokio::spawn(async move { | ||
if let Some(timeout) = timeout { | ||
let _ = fut.with_timeout(timeout).await; | ||
} else { | ||
fut.await; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[cfg(feature = "metrics")] | ||
mod executor; | ||
|
||
#[cfg(feature = "metrics")] | ||
pub use executor::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters