mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-07 00:00:12 +02:00
perf: do not convert to Bytes first in protobuf extractor (#3241)
This commit is contained in:
@@ -3,11 +3,13 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::{rejection::BytesRejection, FromRequest, Request},
|
extract::{rejection::BytesRejection, FromRequest, Request},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
|
RequestExt,
|
||||||
};
|
};
|
||||||
use axum_core::__composite_rejection as composite_rejection;
|
use axum_core::__composite_rejection as composite_rejection;
|
||||||
use axum_core::__define_rejection as define_rejection;
|
use axum_core::__define_rejection as define_rejection;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::BytesMut;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
use http_body_util::BodyExt;
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
|
|
||||||
/// A Protocol Buffer message extractor and response.
|
/// A Protocol Buffer message extractor and response.
|
||||||
@@ -98,10 +100,15 @@ where
|
|||||||
{
|
{
|
||||||
type Rejection = ProtobufRejection;
|
type Rejection = ProtobufRejection;
|
||||||
|
|
||||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request(req: Request, _: &S) -> Result<Self, Self::Rejection> {
|
||||||
let mut bytes = Bytes::from_request(req, state).await?;
|
let mut buf = req
|
||||||
|
.into_limited_body()
|
||||||
|
.collect()
|
||||||
|
.await
|
||||||
|
.map_err(ProtobufDecodeError)?
|
||||||
|
.aggregate();
|
||||||
|
|
||||||
match T::decode(&mut bytes) {
|
match T::decode(&mut buf) {
|
||||||
Ok(value) => Ok(Protobuf(value)),
|
Ok(value) => Ok(Protobuf(value)),
|
||||||
Err(err) => Err(ProtobufDecodeError::from_err(err).into()),
|
Err(err) => Err(ProtobufDecodeError::from_err(err).into()),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user