Skip to content

Commit e5809b7

Browse files
build(deps): update hyper requirement from ^0.14 to ^1.4 (#524)
* build(deps): update hyper requirement from ^0.14 to ^1.4 Updates the requirements on [hyper](https://github.com/hyperium/hyper) to permit the latest version. - [Release notes](https://github.com/hyperium/hyper/releases) - [Changelog](https://github.com/hyperium/hyper/blob/master/CHANGELOG.md) - [Commits](hyperium/hyper@v0.14.0...v1.4.0) --- updated-dependencies: - dependency-name: hyper dependency-type: direct:production ... Signed-off-by: dependabot[bot] <[email protected]> * examples: fix hyper server example Signed-off-by: Luca BRUNO <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Luca BRUNO <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Luca BRUNO <[email protected]>
1 parent 4a0e282 commit e5809b7

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ procfs = { version = "^0.16", optional = true, default-features = false }
3939
[dev-dependencies]
4040
criterion = "0.5"
4141
getopts = "^0.2"
42-
hyper = { version = "^0.14", features = ["server", "http1", "tcp"] }
43-
tokio = { version = "^1.0", features = ["macros", "rt-multi-thread"] }
42+
hyper = { version = "^1.6", features = ["http1", "server"] }
43+
hyper-util = { version = "^0.1", features = ["http1", "server", "tokio"] }
44+
tokio = { version = "^1.0", features = ["macros", "net", "rt-multi-thread"] }
4445

4546
[build-dependencies]
4647
protobuf-codegen = { version = "^3.7.2", optional = true }

examples/example_hyper.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
use hyper::{
4-
header::CONTENT_TYPE,
5-
service::{make_service_fn, service_fn},
6-
Body, Request, Response, Server,
7-
};
8-
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
3+
use std::net::SocketAddr;
94

5+
use hyper::body::Incoming;
6+
use hyper::header::CONTENT_TYPE;
7+
use hyper::server::conn::http1;
8+
use hyper::service::service_fn;
9+
use hyper::Request;
10+
use hyper::Response;
11+
use hyper_util::rt::TokioIo;
1012
use lazy_static::lazy_static;
1113
use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec};
14+
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
15+
use tokio::net::TcpListener;
16+
17+
type BoxedErr = Box<dyn std::error::Error + Send + Sync + 'static>;
1218

1319
lazy_static! {
1420
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
@@ -31,38 +37,39 @@ lazy_static! {
3137
.unwrap();
3238
}
3339

34-
async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
40+
async fn serve_req(_req: Request<Incoming>) -> Result<Response<String>, BoxedErr> {
3541
let encoder = TextEncoder::new();
3642

3743
HTTP_COUNTER.inc();
3844
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["all"]).start_timer();
3945

4046
let metric_families = prometheus::gather();
41-
let mut buffer = vec![];
42-
encoder.encode(&metric_families, &mut buffer).unwrap();
43-
HTTP_BODY_GAUGE.set(buffer.len() as f64);
47+
let body = encoder.encode_to_string(&metric_families)?;
48+
HTTP_BODY_GAUGE.set(body.len() as f64);
4449

4550
let response = Response::builder()
4651
.status(200)
4752
.header(CONTENT_TYPE, encoder.format_type())
48-
.body(Body::from(buffer))
49-
.unwrap();
53+
.body(body)?;
5054

5155
timer.observe_duration();
5256

5357
Ok(response)
5458
}
5559

5660
#[tokio::main]
57-
async fn main() {
58-
let addr = ([127, 0, 0, 1], 9898).into();
61+
async fn main() -> Result<(), BoxedErr> {
62+
let addr: SocketAddr = ([127, 0, 0, 1], 9898).into();
5963
println!("Listening on http://{}", addr);
64+
let listener = TcpListener::bind(addr).await?;
6065

61-
let serve_future = Server::bind(&addr).serve(make_service_fn(|_| async {
62-
Ok::<_, hyper::Error>(service_fn(serve_req))
63-
}));
66+
loop {
67+
let (stream, _) = listener.accept().await?;
68+
let io = TokioIo::new(stream);
6469

65-
if let Err(err) = serve_future.await {
66-
eprintln!("server error: {}", err);
70+
let service = service_fn(serve_req);
71+
if let Err(err) = http1::Builder::new().serve_connection(io, service).await {
72+
eprintln!("server error: {:?}", err);
73+
};
6774
}
6875
}

0 commit comments

Comments
 (0)