Skip to content

Commit

Permalink
Merge pull request Ptrskay3#36 from Ptrskay3/http-body-1.0
Browse files Browse the repository at this point in the history
Update to http-body 1.0
  • Loading branch information
Ptrskay3 authored Nov 27, 2023
2 parents e35bee6 + d04ddcb commit c102992
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 107 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ categories = ["asynchronous", "network-programming", "web-programming", "develop
repository = "https://github.com/Ptrskay3/axum-prometheus"

[dependencies]
axum-core = "0.3.0"
axum = "0.6.1"
axum = "0.7.1"
futures = "0.3.23"
http = "0.2.8"
http-body = "0.4.5"
http = "1.0.0"
http-body = "1.0.0"
metrics = "0.21.0"
metrics-exporter-prometheus = { version = "0.12.0", optional = true }
pin-project = "1.0.12"
tower = "0.4.13"
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"] }
tower-http = "0.4.0"
tower-http = "0.5.0"
bytes = "1.2.1"
futures-core = "0.3.24"
matchit = "0.7"
once_cell = "1.17.0"

[dev-dependencies]
hyper = "0.14.20"
hyper = "1.0.1"
insta = { version = "1.31.0", features = ["yaml", "filters"] }
tower-service = "0.3.2"
http-body-util = "0.1.0"

[features]
default = ["prometheus"]
prometheus = ["metrics-exporter-prometheus"]

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use axum_prometheus::PrometheusMetricLayer;
#[tokio::main]
async fn main() {
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
let app = Router::new()
let app = Router::<()>::new()
.route("/fast", get(|| async {}))
.route(
"/slow",
Expand All @@ -86,11 +86,11 @@ async fn main() {
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(prometheus_layer);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())

let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/builder-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.7.1"
metrics = "0.21.0"
metrics-exporter-prometheus = "0.12"
tokio = { version = "1.0", features = ["full"] }
Expand Down
5 changes: 2 additions & 3 deletions examples/builder-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ async fn main() {
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(prometheus_layer);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
2 changes: 1 addition & 1 deletion examples/endpoint-type-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.7.1"
metrics = "0.21.0"
metrics-exporter-prometheus = "0.12"
tokio = { version = "1.0", features = ["full"] }
Expand Down
5 changes: 2 additions & 3 deletions examples/endpoint-type-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ async fn main() {
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(prometheus_layer);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
4 changes: 2 additions & 2 deletions examples/exporter-statsd-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.7.1"
metrics = "0.21.0"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
metrics-exporter-statsd = "0.5.0"
metrics-exporter-statsd = "0.6.0"
axum-prometheus = { path = "../../", default-features = false }

5 changes: 2 additions & 3 deletions examples/exporter-statsd-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ async fn main() {
.route("/bar", get(|| async {}))
.layer(metric_layer);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
2 changes: 1 addition & 1 deletion examples/simple-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
axum = "0.6.1"
axum = "0.7.1"
metrics = "0.21.0"
metrics-exporter-prometheus = "0.12"
tokio = { version = "1.0", features = ["full"] }
Expand Down
5 changes: 2 additions & 3 deletions examples/simple-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ async fn main() {
.route("/metrics", get(|| async move { metric_handle.render() }))
.layer(prometheus_layer);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@
//! .route("/metrics", get(|| async move { metric_handle.render() }))
//! .layer(prometheus_layer);
//!
//! let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
//! axum::Server::bind(&addr)
//! .serve(app.into_make_service())
//! let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
//! .await
//! .unwrap();
//! axum::serve(listener, app).await.unwrap()
//! }
//! ```
//!
Expand Down Expand Up @@ -515,7 +514,7 @@ where
/// .install_recorder()
/// .unwrap();
///
/// let app = Router::new()
/// let app = Router::<()>::new()
/// .route("/fast", get(|| async {}))
/// .route(
/// "/slow",
Expand All @@ -526,11 +525,11 @@ where
/// .route("/metrics", get(|| async move { metric_handle.render() }))
/// .layer(metric_layer);
///
/// let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
/// let server = axum::Server::bind(&addr)
/// .serve(app.into_make_service());
/// // and to actually run the server:
/// // server.await.unwrap();
/// // Run the server as usual:
/// // let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
/// // .await
/// // .unwrap();
/// // axum::serve(listener, app).await.unwrap()
/// }
/// ```
pub fn new() -> Self {
Expand All @@ -543,6 +542,7 @@ where
}
}

// Enable tracking response body sizes.
pub fn enable_response_body_size(&mut self) {
self.inner_layer.on_body_chunk(Some(BodySizeRecorder));
}
Expand Down Expand Up @@ -591,7 +591,7 @@ where
/// async fn main() {
/// let (metric_layer, metric_handle) = PrometheusMetricLayer::pair();
///
/// let app = Router::new()
/// let app = Router::<()>::new()
/// .route("/fast", get(|| async {}))
/// .route(
/// "/slow",
Expand All @@ -602,11 +602,11 @@ where
/// .route("/metrics", get(|| async move { metric_handle.render() }))
/// .layer(metric_layer);
///
/// let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
/// let server = axum::Server::bind(&addr)
/// .serve(app.into_make_service());
/// // and to actually run the server:
/// // server.await.unwrap();
/// // Run the server as usual:
/// // let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000)))
/// // .await
/// // .unwrap();
/// // axum::serve(listener, app).await.unwrap()
/// }
/// ```
pub fn pair() -> (Self, T) {
Expand Down
75 changes: 35 additions & 40 deletions src/lifecycle/body.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Callbacks, FailedAt, OnBodyChunk};
use futures_core::ready;
use http::HeaderValue;
use http_body::Body;
use http_body::{Body, Frame};
use pin_project::pin_project;
use std::{
fmt,
Expand Down Expand Up @@ -34,67 +34,62 @@ where
type Data = B::Data;
type Error = B::Error;

fn poll_data(
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let this = self.project();

let body_size = this.inner.size_hint().exact();
let Some(result) = ready!(this.inner.poll_data(cx)) else {
return Poll::Ready(None);
};

let body_size = body_size.or_else(|| {
let body_size = this.inner.size_hint().exact().or_else(|| {
this.content_length
.as_ref()
.and_then(|cl| cl.to_str().ok())
.and_then(|cl| cl.parse().ok())
});
let result = ready!(this.inner.poll_frame(cx));

match result {
Ok(chunk) => {
this.on_body_chunk
.call(&chunk, body_size, this.callbacks_data);
Some(Ok(frame)) => {
let frame = match frame.into_data() {
Ok(chunk) => {
this.on_body_chunk
.call(&chunk, body_size, this.callbacks_data);
Frame::data(chunk)
}
Err(frame) => frame,
};

let frame = match frame.into_trailers() {
Ok(trailers) => {
if let Some((classify_eos, callbacks)) = this.parts.take() {
let classification = classify_eos.classify_eos(Some(&trailers));
callbacks.on_eos(
Some(&trailers),
classification,
this.callbacks_data.clone(),
);
}
Frame::trailers(trailers)
}
Err(frame) => frame,
};

Poll::Ready(Some(Ok(chunk)))
Poll::Ready(Some(Ok(frame)))
}
Err(err) => {
Some(Err(err)) => {
if let Some((classify_eos, callbacks)) = this.parts.take() {
let classification = classify_eos.classify_error(&err);
callbacks.on_failure(FailedAt::Body, classification, this.callbacks_data);
}

Poll::Ready(Some(Err(err)))
}
}
}

fn poll_trailers(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
let this = self.project();

let result = ready!(this.inner.poll_trailers(cx));

match result {
Ok(trailers) => {
if let Some((classify_eos, callbacks)) = this.parts.take() {
let trailers = trailers.as_ref();
let classification = classify_eos.classify_eos(trailers);
callbacks.on_eos(trailers, classification, this.callbacks_data.clone());
}

Poll::Ready(Ok(trailers))
}
Err(err) => {
None => {
if let Some((classify_eos, callbacks)) = this.parts.take() {
let classification = classify_eos.classify_error(&err);
callbacks.on_failure(FailedAt::Trailers, classification, this.callbacks_data);
let classification = classify_eos.classify_eos(None);
callbacks.on_eos(None, classification, this.callbacks_data.clone());
}

Poll::Ready(Err(err))
Poll::Ready(None)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lifecycle/future.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum_core::response::Response;
use futures_core::ready;
use http::response::Response;
use http_body::Body;
use pin_project::pin_project;
use std::{
Expand Down
Loading

0 comments on commit c102992

Please sign in to comment.