1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use futures_core::ready;
use pin_project::pin_project;
use std::marker::PhantomData;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tokio::sync::oneshot;
use tower_service::Service;
#[pin_project]
#[derive(Debug)]
pub struct BackgroundReady<T, Request> {
service: Option<T>,
tx: Option<oneshot::Sender<Result<T, crate::BoxError>>>,
_req: PhantomData<Request>,
}
opaque_future! {
pub type ResponseFuture<F, E> = futures_util::future::MapErr<F, fn(E) -> crate::BoxError>;
}
pub(crate) fn background_ready<T, Request>(
service: T,
) -> (
BackgroundReady<T, Request>,
oneshot::Receiver<Result<T, crate::BoxError>>,
)
where
T: Service<Request>,
T::Error: Into<crate::BoxError>,
{
let (tx, rx) = oneshot::channel();
let bg = BackgroundReady {
service: Some(service),
tx: Some(tx),
_req: PhantomData,
};
(bg, rx)
}
impl<T, Request> Future for BackgroundReady<T, Request>
where
T: Service<Request>,
T::Error: Into<crate::BoxError>,
{
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Poll::Ready(_) = this.tx.as_mut().expect("illegal state").poll_closed(cx) {
return Poll::Ready(());
}
let result = ready!(this.service.as_mut().expect("illegal state").poll_ready(cx))
.map(|()| this.service.take().expect("illegal state"));
let _ = this
.tx
.take()
.expect("illegal state")
.send(result.map_err(Into::into));
Poll::Ready(())
}
}