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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::{fmt, future, pin::Pin, task::Context, task::Poll};
use crate::pl_inner::PipelineApi;
use crate::{IntoService, Service, ServiceCaller, util::BoxFuture};
pub use crate::pl_factory::PipelineFactory;
pub use crate::pl_state::{PipelineState, PipelineStateBinding};
/// Execution container for a service and its state.
///
/// A pipeline coordinates readiness, calls, and shutdown for the enclosed
/// service chain.
pub struct Pipeline<Req, Res, Err> {
api: PipelineApi<Req, Res, Err>,
}
/// An independently registered handle to a [`Pipeline`].
///
/// Bindings share the pipeline and its readiness state. Cloning a binding
/// registers another handle.
pub struct PipelineBinding<Req, Res, Err> {
idx: u32,
api: PipelineApi<Req, Res, Err>,
}
impl<Req, Res, Err> Pipeline<Req, Res, Err>
where
Req: 'static,
Res: 'static,
Err: 'static,
{
#[inline]
/// Creates a pipeline containing `service` and `state`.
pub fn new<S, St>(st: St, service: impl IntoService<S, St, Req>) -> Self
where
S: Service<St, Req, Res = Res, Error = Err> + 'static,
St: 'static,
{
Pipeline {
api: PipelineApi::new(service.into_service(), st),
}
}
#[inline]
/// Returns when the pipeline is ready to process requests.
///
/// A successful check is consumed by the next call, which then skips
/// its own readiness check.
pub async fn ready(&self) -> Result<(), Err> {
future::poll_fn(|cx| self.api.poll_ready(cx)).await
}
#[inline]
/// Waits for readiness, then calls the service.
///
/// The readiness check is skipped if the last pipeline readiness check
/// succeeded and no call has started since.
pub async fn call(&self, req: Req) -> Result<Res, Err> {
let pl = self.bind();
pl.api.call(pl.idx, req).await
}
#[inline]
/// Returns an owned future that waits for readiness and calls the service.
///
/// Unlike [`Pipeline::call`], the returned future does not borrow the
/// pipeline and can be moved between local tasks. The readiness check is
/// skipped if the last pipeline readiness check succeeded and no call has
/// started since; this is decided when the future is first polled.
pub fn call_static(&self, req: Req) -> PipelineCall<Req, Res, Err> {
PipelineCall::new(self.bind(), req)
}
#[inline]
/// Returns `Ready` when the pipeline is ready to process requests.
///
/// A successful check is consumed by the next call, which then skips
/// its own readiness check.
pub fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Err>> {
self.api.poll_ready(cx)
}
#[inline]
/// Returns `Ready` when the service has been properly shut down.
pub fn poll_shutdown(&self, cx: &mut Context<'_>) -> Poll<()> {
self.api.poll_shutdown(cx)
}
#[inline]
/// Checks whether pipeline shutdown has been initiated.
pub fn is_shutdown(&self) -> bool {
self.api.is_shutdown()
}
#[inline]
/// Shuts down the enclosed service.
pub async fn shutdown(&self) {
future::poll_fn(|cx| self.api.poll_shutdown(cx)).await;
}
#[inline]
/// Creates a new binding to this pipeline.
///
/// The binding can be used to check readiness and call the service.
pub fn bind(&self) -> PipelineBinding<Req, Res, Err> {
PipelineBinding::new(self)
}
}
impl<Req, Res, Err> ServiceCaller<Req, Res, Err> for Pipeline<Req, Res, Err>
where
Req: 'static,
Res: 'static,
Err: 'static,
{
#[inline]
async fn call_service(&self, req: Req) -> Result<Res, Err> {
let pl = self.bind();
pl.api.call(pl.idx, req).await
}
}
impl<Req, Res, Err> Drop for Pipeline<Req, Res, Err> {
#[inline]
fn drop(&mut self) {
self.api.unreg(0);
}
}
impl<Req, Res, Err> PipelineBinding<Req, Res, Err>
where
Req: 'static,
Res: 'static,
Err: 'static,
{
fn new(pl: &Pipeline<Req, Res, Err>) -> Self {
Self {
idx: pl.api.reg(),
api: pl.api.clone(),
}
}
pub(crate) fn with(idx: u32, api: PipelineApi<Req, Res, Err>) -> Self {
Self { idx, api }
}
#[inline]
/// Waits until the pipeline is ready to process a request.
///
/// A successful check is consumed by the next call, which then skips
/// its own readiness check.
pub async fn ready(&self) -> Result<(), Err> {
self.api.ready(self.idx).await
}
#[inline]
/// Waits for readiness, then calls the service.
///
/// The readiness check is skipped if the last pipeline readiness check
/// succeeded and no call has started since.
pub async fn call(&self, req: Req) -> Result<Res, Err> {
let pl = self.clone();
pl.api.call(pl.idx, req).await
}
#[inline]
/// Returns an owned future that waits for readiness and calls the service.
///
/// The returned future does not borrow this binding and can be moved between
/// local tasks. The readiness check is skipped if the last pipeline readiness
/// check succeeded and no call has started since; this is decided when the
/// future is first polled.
pub fn call_static(&self, req: Req) -> PipelineCall<Req, Res, Err> {
PipelineCall::new(self.clone(), req)
}
}
impl<Req, Res, Err> Drop for PipelineBinding<Req, Res, Err> {
#[inline]
fn drop(&mut self) {
self.api.unreg(self.idx);
}
}
impl<Req, Res, Err> Clone for PipelineBinding<Req, Res, Err> {
fn clone(&self) -> Self {
Self {
idx: self.api.reg(),
api: self.api.clone(),
}
}
}
#[must_use = "futures do nothing unless polled"]
/// An owned future for a pipeline service call.
///
/// Created by [`Pipeline::call_static`] and [`PipelineBinding::call_static`]. The
/// future keeps the pipeline alive. It does not check whether the pipeline is shut
/// down; the request is passed to the service as usual.
pub struct PipelineCall<Req, Res, Err> {
// `fut` borrows from `pl`, so it must be declared (and dropped) first
fut: BoxFuture<'static, Result<Res, Err>>,
#[allow(dead_code)]
pl: PipelineBinding<Req, Res, Err>,
}
impl<Req, Res, Err> PipelineCall<Req, Res, Err> {
#[allow(clippy::missing_transmute_annotations)]
fn new(pl: PipelineBinding<Req, Res, Err>, req: Req) -> Self {
// SAFETY: `fut` borrows from `pl.api` (`Rc`-allocated, never moves).
// `fut` is declared before `pl` in `PipelineCall`, so it is dropped first.
PipelineCall {
fut: unsafe { std::mem::transmute(pl.api.call(pl.idx, req)) },
pl,
}
}
}
impl<Req, Res, Err> future::Future for PipelineCall<Req, Res, Err> {
type Output = Result<Res, Err>;
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.as_mut().fut).poll(cx)
}
}
impl<Req, Res, Err> fmt::Debug for Pipeline<Req, Res, Err> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pipeline").finish()
}
}
impl<Req, Res, Err> fmt::Debug for PipelineBinding<Req, Res, Err> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PipelineBinding")
.field("idx", &self.idx)
.finish()
}
}
impl<Req, Res, Err> fmt::Debug for PipelineCall<Req, Res, Err> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PipelineCall").finish()
}
}