-
Notifications
You must be signed in to change notification settings - Fork 22
feat: retry layer #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: retry layer #378
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f474439
move traits outside of feature flags.
gregorydemay 22f8ecb
Https outcall error trait
gregorydemay f02ec03
retry layer
gregorydemay e91fb03
add test to ensure request IDs are updated
gregorydemay 30b214a
Add error conversion layer
gregorydemay 0a0297a
use retry mechanism
gregorydemay f595f95
fix merge
gregorydemay ef4737e
temporarily disable missing docs
gregorydemay 950900e
generate request ID
gregorydemay d95149b
fix test
gregorydemay 6146eec
Clippy
gregorydemay d12dcf0
test: ensure request id not modified from request endpoint
gregorydemay 636b5fa
test: unit tests for retry
gregorydemay 69b77d8
test: unit tests for Client to use retry
gregorydemay 99eb22d
docs: DoubleMaxResponseBytes
gregorydemay ef9d497
docs
gregorydemay 7a0dba4
move some IC traits to client
gregorydemay e20a57b
add example how to convert errors
gregorydemay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,43 @@ | ||
| use crate::retry::DoubleMaxResponseBytes; | ||
| use crate::{Client, HttpsOutcallError, IcError}; | ||
| use tower::{ServiceBuilder, ServiceExt}; | ||
|
|
||
| // Some middlewares like tower::retry need the underlying service to be cloneable. | ||
| #[test] | ||
| fn should_be_clone() { | ||
| let client = Client::new_with_box_error(); | ||
| let _ = client.clone(); | ||
|
|
||
| let client = Client::new_with_error::<CustomError>(); | ||
| let _ = client.clone(); | ||
| } | ||
|
|
||
| // Note that calling `Client::call` would require a canister environment. | ||
|
gregorydemay marked this conversation as resolved.
|
||
| // We just ensure that the trait bounds are satisfied to have a service. | ||
| #[tokio::test] | ||
| async fn should_be_able_to_use_retry_layer() { | ||
| let mut service = ServiceBuilder::new() | ||
| .retry(DoubleMaxResponseBytes) | ||
| .service(Client::new_with_error::<CustomError>()); | ||
| let _ = service.ready().await.unwrap(); | ||
|
|
||
| let mut service = ServiceBuilder::new() | ||
| .retry(DoubleMaxResponseBytes) | ||
| .service(Client::new_with_box_error()); | ||
| let _ = service.ready().await.unwrap(); | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct CustomError(IcError); | ||
|
|
||
| impl HttpsOutcallError for CustomError { | ||
| fn is_response_too_large(&self) -> bool { | ||
| self.0.is_response_too_large() | ||
| } | ||
| } | ||
|
|
||
| impl From<IcError> for CustomError { | ||
| fn from(value: IcError) -> Self { | ||
| CustomError(value) | ||
| } | ||
| } | ||
This file contains hidden or 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,115 @@ | ||
| use pin_project::pin_project; | ||
| use std::future::Future; | ||
| use std::marker::PhantomData; | ||
| use std::pin::Pin; | ||
| use std::task::{Context, Poll}; | ||
| use tower::Service; | ||
| use tower_layer::Layer; | ||
|
|
||
| /// Convert error of a service into another type, where the conversion does *not* fail. | ||
| /// | ||
| /// This [`Layer`] produces instances of the [`ConvertError`] service. | ||
| /// | ||
| /// [`Layer`]: tower::Layer | ||
| #[derive(Debug)] | ||
| pub struct ConvertErrorLayer<E> { | ||
| _marker: PhantomData<E>, | ||
| } | ||
|
|
||
| impl<E> ConvertErrorLayer<E> { | ||
| /// Returns a new [`ConvertErrorLayer`] | ||
| pub fn new() -> Self { | ||
| Self { | ||
| _marker: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<E> Default for ConvertErrorLayer<E> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl<E> Clone for ConvertErrorLayer<E> { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| _marker: self._marker, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Convert the inner service error to another type, where the conversion does *not* fail. | ||
| #[derive(Debug)] | ||
| pub struct ConvertError<S, E> { | ||
| inner: S, | ||
| _marker: PhantomData<E>, | ||
| } | ||
|
|
||
| impl<S: Clone, E> Clone for ConvertError<S, E> { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| inner: self.inner.clone(), | ||
| _marker: self._marker, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S, E> Layer<S> for ConvertErrorLayer<E> { | ||
| type Service = ConvertError<S, E>; | ||
|
|
||
| fn layer(&self, inner: S) -> Self::Service { | ||
| Self::Service { | ||
| inner, | ||
| _marker: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S, Request, Error, NewError> Service<Request> for ConvertError<S, NewError> | ||
| where | ||
| S: Service<Request, Error = Error>, | ||
| Error: Into<NewError>, | ||
| { | ||
| type Response = S::Response; | ||
| type Error = NewError; | ||
| type Future = ResponseFuture<S::Future, NewError>; | ||
|
|
||
| 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: Request) -> Self::Future { | ||
| ResponseFuture { | ||
| response_future: self.inner.call(req), | ||
| _marker: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[pin_project] | ||
| pub struct ResponseFuture<F, NewError> { | ||
| #[pin] | ||
| response_future: F, | ||
| _marker: PhantomData<NewError>, | ||
| } | ||
|
|
||
| impl<F, Response, Error, NewError> Future for ResponseFuture<F, NewError> | ||
| where | ||
| F: Future<Output = Result<Response, Error>>, | ||
| Error: Into<NewError>, | ||
| { | ||
| type Output = Result<Response, NewError>; | ||
|
|
||
| fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let this = self.project(); | ||
| let result_fut = this.response_future.poll(cx); | ||
| match result_fut { | ||
| Poll::Ready(result) => match result { | ||
| Ok(response) => Poll::Ready(Ok(response)), | ||
| Err(e) => Poll::Ready(Err(e.into())), | ||
| }, | ||
| Poll::Pending => Poll::Pending, | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some subtle issues here at play, which is the reason why I needed yet another type of conversion to somewhat emulate
map_errbut with a concrete type:tower::retry, the service needs to beClone.impl Somethingcannot beClone.map_erronly takes a closure (FnOnce) and unfortunately no concrete type can implement theFnOncetrait (see Rust issue 29625).BoxServiceorBoxCloneServiceonClientto do type erasure but this also won't work because it requires the future type of the service to beSend + 'staticwhich cannot be derived because the future ofClientis not exposed by theic_cdkso we have to resort to using<Box<dyn Future>>All of that to say feel free to chime in if you have better ideas 🙈 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't say I have a better idea... Maybe @ninegua ?