-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recover from timeout errors in the service
- Loading branch information
1 parent
3c5d8c5
commit 214c948
Showing
6 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::{body::BoxBody, Status}; | ||
use futures_util::ready; | ||
use http::Response; | ||
use pin_project::pin_project; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use tower::Service; | ||
|
||
/// Middleware that attempts to recover from service errors by turning them into a response with a | ||
/// `grpc-status` and an empty body. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct RecoverError<S> { | ||
inner: S, | ||
} | ||
|
||
impl<S> RecoverError<S> { | ||
pub(crate) fn new(inner: S) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl<S, R> Service<R> for RecoverError<S> | ||
where | ||
S: Service<R, Response = Response<BoxBody>>, | ||
S::Error: Into<crate::Error>, | ||
{ | ||
type Response = Response<BoxBody>; | ||
type Error = crate::Error; | ||
type Future = ResponseFuture<S::Future>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
fn call(&mut self, req: R) -> Self::Future { | ||
ResponseFuture { | ||
inner: self.inner.call(req), | ||
} | ||
} | ||
} | ||
|
||
#[pin_project] | ||
pub(crate) struct ResponseFuture<F> { | ||
#[pin] | ||
inner: F, | ||
} | ||
|
||
impl<F, E> Future for ResponseFuture<F> | ||
where | ||
F: Future<Output = Result<Response<BoxBody>, E>>, | ||
E: Into<crate::Error>, | ||
{ | ||
type Output = Result<Response<BoxBody>, crate::Error>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let result: Result<Response<BoxBody>, crate::Error> = | ||
ready!(self.project().inner.poll(cx)).map_err(Into::into); | ||
|
||
match result { | ||
Ok(res) => Poll::Ready(Ok(res)), | ||
Err(err) => { | ||
if let Some(status) = Status::try_from_error(&*err) { | ||
let mut res = Response::new(BoxBody::empty()); | ||
status.add_header(res.headers_mut()).unwrap(); | ||
Poll::Ready(Ok(res)) | ||
} else { | ||
Poll::Ready(Err(err)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters