-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ext: add ServiceExt::oneshot to call the service when it is ready (#164)
- Loading branch information
1 parent
f423389
commit 0dc8281
Showing
2 changed files
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::mem; | ||
|
||
use futures::{Async, Future, Poll}; | ||
use tower_service::Service; | ||
|
||
/// A `Future` consuming a `Service` and request, waiting until the `Service` | ||
/// is ready, and then calling `Service::call` with the request, and | ||
/// waiting for that `Future`. | ||
pub struct Oneshot<S: Service<Req>, Req> { | ||
state: State<S, Req>, | ||
} | ||
|
||
enum State<S: Service<Req>, Req> { | ||
NotReady(S, Req), | ||
Called(S::Future), | ||
Tmp, | ||
} | ||
|
||
impl<S, Req> Oneshot<S, Req> | ||
where | ||
S: Service<Req>, | ||
{ | ||
pub(super) fn new(svc: S, req: Req) -> Self { | ||
Oneshot { | ||
state: State::NotReady(svc, req), | ||
} | ||
} | ||
} | ||
|
||
impl<S, Req> Future for Oneshot<S, Req> | ||
where | ||
S: Service<Req>, | ||
{ | ||
type Item = S::Response; | ||
type Error = S::Error; | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
loop { | ||
match mem::replace(&mut self.state, State::Tmp) { | ||
State::NotReady(mut svc, req) => match svc.poll_ready()? { | ||
Async::Ready(()) => { | ||
self.state = State::Called(svc.call(req)); | ||
} | ||
Async::NotReady => { | ||
self.state = State::NotReady(svc, req); | ||
return Ok(Async::NotReady); | ||
} | ||
}, | ||
State::Called(mut fut) => match fut.poll()? { | ||
Async::Ready(res) => { | ||
return Ok(Async::Ready(res)); | ||
} | ||
Async::NotReady => { | ||
self.state = State::Called(fut); | ||
return Ok(Async::NotReady); | ||
} | ||
}, | ||
State::Tmp => panic!("polled after complete"), | ||
} | ||
} | ||
} | ||
} |