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

util: fix oneshot dropping pending services immediately #447

Merged
merged 2 commits into from
Apr 23, 2020
Merged
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
14 changes: 7 additions & 7 deletions tower/src/util/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Oneshot<S: Service<Req>, Req> {

#[pin_project]
enum State<S: Service<Req>, Req> {
NotReady(Option<(S, Req)>),
NotReady(S, Option<Req>),
Called(#[pin] S::Future),
Done,
}
Expand All @@ -32,12 +32,12 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
State::NotReady(Some((s, req))) => f
State::NotReady(s, Some(req)) => f
.debug_tuple("State::NotReady")
.field(s)
.field(req)
.finish(),
State::NotReady(None) => unreachable!(),
State::NotReady(_, None) => unreachable!(),
State::Called(_) => f.debug_tuple("State::Called").field(&"S::Future").finish(),
State::Done => f.debug_tuple("State::Done").finish(),
}
Expand All @@ -51,7 +51,7 @@ where
#[allow(missing_docs)]
pub fn new(svc: S, req: Req) -> Self {
Oneshot {
state: State::NotReady(Some((svc, req))),
state: State::NotReady(svc, Some(req)),
}
}
}
Expand All @@ -68,10 +68,10 @@ where
loop {
#[project]
match this.state.as_mut().project() {
State::NotReady(nr) => {
let (mut svc, req) = nr.take().expect("We immediately transition to ::Called");
State::NotReady(svc, req) => {
let _ = ready!(svc.poll_ready(cx))?;
this.state.set(State::Called(svc.call(req)));
let f = svc.call(req.take().expect("already called"));
this.state.set(State::Called(f));
}
State::Called(fut) => {
let res = ready!(fut.poll(cx))?;
Expand Down
1 change: 1 addition & 0 deletions tower/tests/util/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg(feature = "util")]

mod call_all;
mod oneshot;
mod service_fn;
39 changes: 39 additions & 0 deletions tower/tests/util/oneshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::task::{Context, Poll};
use std::{future::Future, pin::Pin};
use tower::util::ServiceExt;
use tower_service::Service;

#[tokio::test]
async fn service_driven_to_readiness() {
// This test ensures that `oneshot` will repeatedly call `poll_ready` until
// the service is ready.

struct PollMeTwice {
ready: bool,
};
impl Service<()> for PollMeTwice {
type Error = ();
type Response = ();
type Future = Pin<
Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'static>,
>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), ()>> {
if self.ready {
Poll::Ready(Ok(()))
} else {
self.ready = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}

fn call(&mut self, _: ()) -> Self::Future {
assert!(self.ready, "service not driven to readiness!");
Box::pin(async { Ok(()) })
}
}

let svc = PollMeTwice { ready: false };
svc.oneshot(()).await;
}