Skip to content

Commit

Permalink
feat: http crate (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
heilhead authored Jul 13, 2023
1 parent 940f8d8 commit 99e610e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ full = [
"profiler",
"collections",
"future",
"http",
"metrics",
]
alloc = ["dep:alloc"]
collections = ["dep:collections"]
future = ["dep:future"]
metrics = ["dep:metrics", "future/metrics", "alloc/metrics"]
http = []
metrics = ["dep:metrics", "future/metrics", "alloc/metrics", "http/metrics"]
profiler = ["alloc/profiler"]

[dependencies]
alloc = { path = "./crates/alloc", optional = true }
collections = { path = "./crates/collections", optional = true }
future = { path = "./crates/future", optional = true }
http = { path = "./crates/http", optional = true }
metrics = { path = "./crates/metrics", optional = true }

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Extensions for collections such as `HashMap`.

Convenience `Future` extensions.

## `http`

Metrics and other utils for HTTP servers.

## `metrics`

Global service metrics. Currently based on `opentelemetry` SDK and exported in `prometheus` format.
Expand Down
15 changes: 15 additions & 0 deletions crates/http/Cargo.toml
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"] }
59 changes: 59 additions & 0 deletions crates/http/src/executor.rs
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;
}
});
}
}
5 changes: 5 additions & 0 deletions crates/http/src/lib.rs
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::*;
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ pub use alloc;
pub use collections;
#[cfg(feature = "future")]
pub use future;
#[cfg(feature = "http")]
pub use http;
#[cfg(feature = "metrics")]
pub use metrics;

0 comments on commit 99e610e

Please sign in to comment.