Skip to content

Commit 40fb659

Browse files
authored
chore(server): Refactor recover error service (#2100)
1 parent 36bc21c commit 40fb659

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

tonic/src/transport/server/service/recover_error.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use crate::Status;
2-
use http::Response;
3-
use http_body::Frame;
4-
use pin_project::pin_project;
51
use std::{
62
future::Future,
73
pin::Pin,
84
task::{ready, Context, Poll},
95
};
10-
use tower::Service;
6+
7+
use http::Response;
8+
use pin_project::pin_project;
9+
use tower_service::Service;
10+
11+
use crate::Status;
1112

1213
/// Middleware that attempts to recover from service errors by turning them into a response built
1314
/// from the `Status`.
@@ -22,20 +23,20 @@ impl<S> RecoverError<S> {
2223
}
2324
}
2425

25-
impl<S, R, ResBody> Service<R> for RecoverError<S>
26+
impl<S, Req, ResBody> Service<Req> for RecoverError<S>
2627
where
27-
S: Service<R, Response = Response<ResBody>>,
28+
S: Service<Req, Response = Response<ResBody>>,
2829
S::Error: Into<crate::BoxError>,
2930
{
30-
type Response = Response<MaybeEmptyBody<ResBody>>;
31+
type Response = Response<ResponseBody<ResBody>>;
3132
type Error = crate::BoxError;
3233
type Future = ResponseFuture<S::Future>;
3334

3435
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
3536
self.inner.poll_ready(cx).map_err(Into::into)
3637
}
3738

38-
fn call(&mut self, req: R) -> Self::Future {
39+
fn call(&mut self, req: Req) -> Self::Future {
3940
ResponseFuture {
4041
inner: self.inner.call(req),
4142
}
@@ -53,21 +54,18 @@ where
5354
F: Future<Output = Result<Response<ResBody>, E>>,
5455
E: Into<crate::BoxError>,
5556
{
56-
type Output = Result<Response<MaybeEmptyBody<ResBody>>, crate::BoxError>;
57+
type Output = Result<Response<ResponseBody<ResBody>>, crate::BoxError>;
5758

5859
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
59-
let result: Result<Response<_>, crate::BoxError> =
60-
ready!(self.project().inner.poll(cx)).map_err(Into::into);
61-
62-
match result {
60+
match ready!(self.project().inner.poll(cx)) {
6361
Ok(response) => {
64-
let response = response.map(MaybeEmptyBody::full);
62+
let response = response.map(ResponseBody::full);
6563
Poll::Ready(Ok(response))
6664
}
67-
Err(err) => match Status::try_from_error(err) {
65+
Err(err) => match Status::try_from_error(err.into()) {
6866
Ok(status) => {
6967
let (parts, ()) = status.into_http::<()>().into_parts();
70-
let res = Response::from_parts(parts, MaybeEmptyBody::empty());
68+
let res = Response::from_parts(parts, ResponseBody::empty());
7169
Poll::Ready(Ok(res))
7270
}
7371
Err(err) => Poll::Ready(Err(err)),
@@ -77,22 +75,22 @@ where
7775
}
7876

7977
#[pin_project]
80-
pub(crate) struct MaybeEmptyBody<B> {
78+
pub(crate) struct ResponseBody<B> {
8179
#[pin]
8280
inner: Option<B>,
8381
}
8482

85-
impl<B> MaybeEmptyBody<B> {
83+
impl<B> ResponseBody<B> {
8684
fn full(inner: B) -> Self {
8785
Self { inner: Some(inner) }
8886
}
8987

90-
fn empty() -> Self {
88+
const fn empty() -> Self {
9189
Self { inner: None }
9290
}
9391
}
9492

95-
impl<B> http_body::Body for MaybeEmptyBody<B>
93+
impl<B> http_body::Body for ResponseBody<B>
9694
where
9795
B: http_body::Body,
9896
{
@@ -102,7 +100,7 @@ where
102100
fn poll_frame(
103101
self: Pin<&mut Self>,
104102
cx: &mut Context<'_>,
105-
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
103+
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
106104
match self.project().inner.as_pin_mut() {
107105
Some(b) => b.poll_frame(cx),
108106
None => Poll::Ready(None),

0 commit comments

Comments
 (0)