Skip to content

Commit b8aa5d6

Browse files
authored
refactor: Upgrade to hyper 1.0 (#1858)
## Description This upgrades our direct use of hyper to version 1.0 of the crate. ## Notes & open questions This is adds a little duplication to our deps for now since reqwest is still using hyper 1.0. ## Change checklist - [x] Self-review. - [x] Documentation updates if relevant. - [x] Tests if relevant.
1 parent b95eb86 commit b8aa5d6

File tree

9 files changed

+348
-293
lines changed

9 files changed

+348
-293
lines changed

Cargo.lock

+103-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-metrics/Cargo.toml

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ rust-version = "1.72"
1515
workspace = true
1616

1717
[dependencies]
18-
prometheus-client = { version = "0.22.0", optional = true }
19-
once_cell = "1.17.0"
20-
tracing = "0.1"
21-
hyper = { version = "0.14.25", features = ["server", "client", "http1", "tcp"] }
18+
anyhow = "1.0.75"
2219
erased_set = "0.7"
20+
http-body-util = "0.1.0"
21+
hyper = { version = "1", features = ["server", "http1"] }
22+
hyper-util = { version = "0.1.1", features = ["tokio"] }
23+
once_cell = "1.17.0"
24+
prometheus-client = { version = "0.22.0", optional = true }
2325
struct_iterable = "0.1"
26+
tokio = { version = "1", features = ["rt", "net"]}
27+
tracing = "0.1"
2428

2529
[dev-dependencies]
2630
tokio = { version = "1", features = ["io-util", "sync", "rt", "net", "fs", "macros", "time", "test-util"] }

iroh-metrics/src/metrics.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@
4444
//! inc!(Metrics, things_added);
4545
//! ```
4646
47-
#[cfg(feature = "metrics")]
48-
use hyper::Error;
47+
// TODO: move cfg to lib.rs
4948
#[cfg(feature = "metrics")]
5049
use std::net::SocketAddr;
5150

5251
/// Start a server to serve the OpenMetrics endpoint.
5352
#[cfg(feature = "metrics")]
54-
pub async fn start_metrics_server(addr: SocketAddr) -> Result<(), Error> {
53+
pub async fn start_metrics_server(addr: SocketAddr) -> anyhow::Result<()> {
5554
crate::service::run(addr).await
5655
}

iroh-metrics/src/service.rs

+36-34
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
use std::{future::Future, io, net::SocketAddr, pin::Pin};
1+
use std::net::SocketAddr;
22

3-
use hyper::{
4-
service::{make_service_fn, service_fn},
5-
Body, Error, Request, Response, Server,
6-
};
7-
8-
use tracing::info;
3+
use anyhow::{anyhow, Result};
4+
use hyper::service::service_fn;
5+
use hyper::{Request, Response};
6+
use tokio::net::TcpListener;
7+
use tracing::{error, info};
98

109
use crate::core::Core;
1110

11+
type BytesBody = http_body_util::Full<hyper::body::Bytes>;
12+
1213
/// Start a HTTP server to report metrics.
13-
pub async fn run(metrics_addr: SocketAddr) -> Result<(), Error> {
14+
pub async fn run(metrics_addr: SocketAddr) -> Result<()> {
1415
info!("Starting metrics server on {metrics_addr}");
15-
Server::bind(&metrics_addr)
16-
.serve(make_service_fn(move |_conn| async move {
17-
let handler = make_handler();
18-
Ok::<_, io::Error>(service_fn(handler))
19-
}))
20-
.await
16+
let listener = TcpListener::bind(metrics_addr).await?;
17+
loop {
18+
let (stream, _addr) = listener.accept().await?;
19+
let io = hyper_util::rt::TokioIo::new(stream);
20+
tokio::spawn(async move {
21+
if let Err(err) = hyper::server::conn::http1::Builder::new()
22+
.serve_connection(io, service_fn(handler))
23+
.await
24+
{
25+
error!("Error serving metrics connection: {err:#}");
26+
}
27+
});
28+
}
2129
}
2230

23-
/// This function returns an HTTP handler fn that will respond with the
24-
/// OpenMetrics encoding of our metrics.
25-
fn make_handler(
26-
) -> impl Fn(Request<Body>) -> Pin<Box<dyn Future<Output = io::Result<Response<Body>>> + Send>> {
27-
// This closure accepts a request and responds with the OpenMetrics encoding of our metrics.
28-
move |_req: Request<Body>| {
29-
Box::pin(async move {
30-
let core = Core::get()
31-
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "metrics disabled"))?;
32-
core.encode()
33-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
34-
.map(|r| {
35-
let body = Body::from(r);
36-
Response::builder()
37-
.header(hyper::header::CONTENT_TYPE, "text/plain; charset=utf-8")
38-
.body(body)
39-
.expect("Failed to build response")
40-
})
41-
})
42-
}
31+
/// HTTP handler that will respond with the OpenMetrics encoding of our metrics.
32+
async fn handler(_req: Request<hyper::body::Incoming>) -> Result<Response<BytesBody>> {
33+
let core = Core::get().ok_or_else(|| anyhow!("metrics disabled"))?;
34+
core.encode().map_err(anyhow::Error::new).map(|r| {
35+
Response::builder()
36+
.header(hyper::header::CONTENT_TYPE, "text/plain; charset=utf-8")
37+
.body(body_full(r))
38+
.expect("Failed to build response")
39+
})
40+
}
41+
42+
/// Creates a new [`BytesBody`] with given content.
43+
fn body_full(content: impl Into<hyper::body::Bytes>) -> BytesBody {
44+
http_body_util::Full::new(content.into())
4345
}

iroh-net/Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ ed25519-dalek = { version = "2.0.0", features = ["serde", "rand_core"] }
2929
flume = "0.11"
3030
futures = "0.3.25"
3131
governor = "0.6.0"
32-
iroh-base = { version = "0.11.0", path = "../iroh-base" }
3332
hex = "0.4.3"
3433
hostname = "0.3.1"
35-
http = "0.2.9"
36-
hyper = { version = "0.14.25", features = ["server", "client", "http1", "tcp"] }
34+
http = "1"
35+
http-body-util = "0.1.0"
36+
hyper = { version = "1", features = ["server", "client", "http1"] }
37+
hyper-util = "0.1.1"
3738
igd = { version = "0.12.1", features = ["aio"] }
39+
iroh-base = { version = "0.11.0", path = "../iroh-base" }
3840
libc = "0.2.139"
3941
num_enum = "0.7"
4042
once_cell = "1.18.0"

0 commit comments

Comments
 (0)