Skip to content
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

fix(examples): Fix tower examples #547

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/src/tower/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use futures::TryFutureExt;
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use service::AuthSvc;
Expand Down Expand Up @@ -57,13 +58,9 @@ mod service {
}

fn call(&mut self, req: Request<BoxBody>) -> Self::Future {
let mut channel = self.inner.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually want to keep it this way because it shows how you can embed async work around the future of the channel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another solution which we can apply.

let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
    // Do extra async work here...
    channel.call(req).await.map_err(Into::into)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting.....maybe the mem replace works? @olix0r what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is fine?

// Do extra async work here...

Box::pin(async move {
// Do extra async work here...

channel.call(req).await.map_err(Into::into)
})
Box::pin(self.inner.call(req).err_into::<Self::Error>())
Comment on lines +61 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Do extra async work here...
Box::pin(async move {
// Do extra async work here...
channel.call(req).await.map_err(Into::into)
})
Box::pin(self.inner.call(req).err_into::<Self::Error>())
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
// Do extra async work here...
inner.call(req).await.map_err::<Self::Error>()
})

This was we still make it clear to users where to add additional async code.

}
}
}
9 changes: 3 additions & 6 deletions examples/src/tower/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use futures::TryFutureExt;
use hyper::{Body, Request as HyperRequest, Response as HyperResponse};
use std::task::{Context, Poll};
use tonic::{
Expand Down Expand Up @@ -71,13 +72,9 @@ where
}

fn call(&mut self, req: HyperRequest<Body>) -> Self::Future {
let mut svc = self.inner.clone();
// Do async work here....

Box::pin(async move {
// Do async work here....

svc.call(req).await
})
Box::pin(self.inner.call(req).err_into::<Self::Error>())
Comment on lines +75 to +77
Copy link
Member

@davidpdrsn davidpdrsn Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Do async work here....
Box::pin(async move {
// Do async work here....
svc.call(req).await
})
Box::pin(self.inner.call(req).err_into::<Self::Error>())
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
// Do extra async work here...
inner.call(req).await.map_err::<Self::Error>()
})

}
}

Expand Down