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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::{fmt, marker::PhantomPinned, ptr, rc::Rc, task::Context, task::Poll};
use crate::{
BindCtx, Ctx, IntoService, ReadyCallback, ReadyLink, Service, ctx::WaitersRef, util::BoxFuture,
};
use crate::pipeline::PipelineBinding;
use crate::pl_inner::{PipelineApi, PipelineInternalApi, PipelineReadiness};
use crate::readiness::ReadyScope;
/// Execution container for a service whose state is supplied per operation.
///
/// Unlike [`crate::Pipeline`], this type does not own a state value. Callers
/// provide a state reference when checking readiness, calling, or shutting
/// down the service.
///
/// All bindings share one readiness cache and checker, including bindings with
/// different state values. Readiness may inspect state, but must describe the
/// shared pipeline-wide gate. Independent gates require separate pipelines.
pub struct PipelineState<St, Req, Res, Err> {
api: Rc<dyn PipelineStateApi<St, Req, Res, Err>>,
}
impl<St, Req, Res, Err> PipelineState<St, Req, Res, Err>
where
St: 'static,
Req: 'static,
Res: 'static,
Err: 'static,
{
#[inline]
/// Creates a state-independent pipeline containing `service`.
pub fn new<S>(service: impl IntoService<S, St, Req>) -> Self
where
S: Service<St, Req, Res = Res, Error = Err> + 'static,
St: 'static,
{
let waiters = WaitersRef::new();
let s = service.into_service();
s.bind(BindCtx::new(waiters.scope()));
PipelineState {
api: Rc::new(PipelineInner {
s,
readiness: PipelineReadiness::new(waiters.scope()),
waiters,
_pinned: PhantomPinned,
}),
}
}
#[inline]
/// Returns when the pipeline is ready to process requests.
///
/// Successful readiness is shared and reused until a subscribed
/// [`crate::ReadyNotifier`] invalidates it. Calls do not consume it.
/// Reusing valid readiness does not allocate a readiness future.
///
/// The first caller that finds readiness invalid runs the shared check in its
/// own future, and other waiters wait for it. Like a held lock, a pending
/// future must be polled or dropped: keeping it alive without polling stalls
/// every other readiness waiter on this pipeline.
pub async fn ready(&self, st: &St) -> Result<(), Err> {
if self.is_ready() {
Ok(())
} else {
self.api.ready(0, st).await
}
}
#[inline]
/// Returns `true` while the pipeline's shared readiness is valid.
///
/// Does not run or register a readiness check. `false` means readiness is
/// unknown or was invalidated, not that the service is not ready.
pub fn is_ready(&self) -> bool {
self.api.scope().is_ready()
}
#[inline]
/// Waits for readiness, then calls the service with `st`.
///
/// Reuses valid shared readiness without consuming it.
pub async fn call(&self, req: Req, st: &St) -> Result<Res, Err> {
let pl = self.binding();
self.api.call(pl.idx, req, st).await
}
#[inline]
/// Shuts down the enclosed service.
pub async fn shutdown(&self, st: &St) {
self.api.shutdown(0, st).await;
}
#[inline]
/// Returns `Ready` when the pipeline is ready to process requests.
///
/// Registers the task for readiness changes and reuses valid shared
/// readiness. A pending check is retained by the pipeline, even if this
/// task stops polling; other callers can continue driving it.
/// The retained check and its wake sources run on this pipeline's thread.
/// Its checker wakers must also be finally dropped on that thread, even
/// after the pipeline is dropped; foreign wakes or final drops panic.
///
/// # Panics
///
/// Panics if `.shutdown()` has been called. Unlike [`crate::Pipeline::poll_ready`],
/// it does not return `Ready(Ok(()))` after shutdown.
pub fn poll_ready(&self, cx: &mut Context<'_>, st: &St) -> Poll<Result<(), Err>>
where
St: Clone,
{
self.api.poll_ready(cx, st)
}
fn binding(&self) -> Binding<'_, St, Req, Res, Err> {
Binding {
idx: self.api.reg(),
api: self.api.as_ref(),
}
}
#[inline]
/// Creates a binding that accepts state per call.
///
/// The binding can be used to call the service.
pub fn bind(&self) -> PipelineStateBinding<St, Req, Res, Err> {
PipelineStateBinding {
idx: self.api.reg(),
api: self.api.clone(),
}
}
#[inline]
/// Creates a standard pipeline binding by attaching an owned state value.
///
/// The binding can be used to call the service.
pub fn bind_state(&self, st: St) -> PipelineBinding<Req, Res, Err>
where
St: Clone,
{
let internal = PipelineInternal {
st,
api: self.api.clone(),
};
PipelineBinding::with(self.api.reg(), PipelineApi::with(internal))
}
#[inline]
/// Registers a callback for notifications reaching this pipeline's scope.
///
/// Registration is shared by all bindings, regardless of their state, and
/// persists until the scope is dropped. Repeated registration of the same
/// handle is idempotent. The callback receives `0` for this root scope.
/// See [`ReadyCallback`] for notification semantics.
pub fn on_notify(&self, callback: &ReadyCallback) {
self.api.scope().on_notify(callback);
}
#[inline]
/// Registers this pipeline as a readiness dependency of the enclosing service.
///
/// Call from [`Service::bind`] so changes to this pipeline's readiness also
/// invalidate the enclosing scope. Repeated registration is idempotent and
/// does not rebind the inner service or create another runtime handle.
pub fn bind_to(&self, ctx: &BindCtx<'_>) {
ctx.subscribe_pipeline(&self.readiness_link());
}
/// Returns this pipeline's readiness endpoint, shared by all state bindings.
///
/// Register with [`BindCtx::subscribe_pipeline`]. The link propagates
/// invalidation without checking readiness or retaining the pipeline.
pub fn readiness_link(&self) -> ReadyLink {
self.api.scope().readiness_link()
}
}
impl<St, Req, Res, Err> Drop for PipelineState<St, Req, Res, Err> {
#[inline]
fn drop(&mut self) {
self.api.unreg(0);
}
}
struct Binding<'a, St, Req, Res, Err> {
idx: u32,
api: &'a dyn PipelineStateApi<St, Req, Res, Err>,
}
impl<St, Req, Res, Err> Drop for Binding<'_, St, Req, Res, Err> {
#[inline]
fn drop(&mut self) {
self.api.unreg(self.idx);
}
}
// ========================== `PipelineStateBinding` ===========================
/// An independently registered handle to a [`PipelineState`].
pub struct PipelineStateBinding<St, Req, Res, Err> {
idx: u32,
api: Rc<dyn PipelineStateApi<St, Req, Res, Err>>,
}
impl<St, Req, Res, Err> Drop for PipelineStateBinding<St, Req, Res, Err> {
#[inline]
fn drop(&mut self) {
self.api.unreg(self.idx);
}
}
impl<St, Req, Res, Err> Clone for PipelineStateBinding<St, Req, Res, Err> {
#[inline]
fn clone(&self) -> Self {
PipelineStateBinding {
idx: self.api.reg(),
api: self.api.clone(),
}
}
}
impl<St, Req, Res, Err> PipelineStateBinding<St, Req, Res, Err>
where
St: 'static,
Req: 'static,
Res: 'static,
Err: 'static,
{
#[inline]
/// Waits for readiness, then calls the service with `st`.
///
/// Reuses valid shared readiness without consuming it, regardless of `st`.
pub async fn call(&self, req: Req, st: &St) -> Result<Res, Err> {
let pl = Binding {
idx: self.api.reg(),
api: self.api.as_ref(),
};
pl.api.call(pl.idx, req, st).await
}
#[inline]
/// Registers a callback on the shared pipeline scope, independently of state.
///
/// The callback receives `0` for this root scope.
/// Dropping this binding does not remove the registration.
/// See [`PipelineState::on_notify`] and [`ReadyCallback`] for details.
pub fn on_notify(&self, callback: &ReadyCallback) {
self.api.scope().on_notify(callback);
}
#[inline]
/// Registers this handle's pipeline as a readiness dependency.
///
/// Call from [`Service::bind`]. See [`PipelineState::bind_to`] for details.
pub fn bind_to(&self, ctx: &BindCtx<'_>) {
ctx.subscribe_pipeline(&self.readiness_link());
}
/// Returns the shared pipeline's readiness endpoint.
///
/// See [`PipelineState::readiness_link`] and [`ReadyLink`].
pub fn readiness_link(&self) -> ReadyLink {
self.api.scope().readiness_link()
}
}
// ========================== `PipelineApi` ===========================
struct PipelineInternal<St, Req, Res, Err> {
st: St,
api: Rc<dyn PipelineStateApi<St, Req, Res, Err>>,
}
impl<St, Req, Res, Err> PipelineInternalApi<Req, Res, Err> for PipelineInternal<St, Req, Res, Err> {
fn scope(&self) -> &ReadyScope {
self.api.scope()
}
fn reg(&self) -> u32 {
self.api.reg()
}
fn unreg(&self, idx: u32) {
self.api.unreg(idx);
}
fn ready(&self, idx: u32) -> BoxFuture<'_, Result<(), Err>> {
self.api.ready(idx, &self.st)
}
fn call(&self, idx: u32, req: Req) -> BoxFuture<'_, Result<Res, Err>> {
self.api.call(idx, req, &self.st)
}
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Err>> {
unreachable!()
}
fn poll_shutdown(&self, _: &mut Context<'_>) -> Poll<()> {
unreachable!()
}
fn is_shutdown(&self) -> bool {
self.api.is_shutdown()
}
}
// ========================== `PipelineStateApi` ===========================
struct PipelineInner<S, E> {
s: S,
waiters: WaitersRef,
readiness: PipelineReadiness<E>,
// The pending readiness future borrows the other fields. `!Unpin` keeps
// `&mut self` in `Drop` from invalidating those borrows.
_pinned: PhantomPinned,
}
impl<S, E> Drop for PipelineInner<S, E> {
fn drop(&mut self) {
// The readiness future borrows `s` and `waiters`, so it must be dropped
// before the fields it references
self.readiness.cancel();
}
}
trait PipelineStateApi<St, Req, Res, Err> {
fn scope(&self) -> &ReadyScope;
fn reg(&self) -> u32;
fn unreg(&self, idx: u32);
fn call<'a>(&'a self, idx: u32, req: Req, st: &'a St) -> BoxFuture<'a, Result<Res, Err>>
where
Req: 'a;
fn ready<'a>(&'a self, idx: u32, st: &'a St) -> BoxFuture<'a, Result<(), Err>>
where
Req: 'a;
fn poll_ready(&self, cx: &mut Context<'_>, st: &St) -> Poll<Result<(), Err>>
where
St: Clone;
fn shutdown<'a>(&'a self, idx: u32, st: &'a St) -> BoxFuture<'a, ()>;
fn is_shutdown(&self) -> bool;
}
impl<S, St, Req, E> PipelineStateApi<St, Req, S::Res, S::Error> for PipelineInner<S, E>
where
S: Service<St, Req, Error = E> + 'static,
St: 'static,
Req: 'static,
E: 'static,
{
fn scope(&self) -> &ReadyScope {
self.waiters.scope()
}
fn reg(&self) -> u32 {
self.waiters.insert()
}
fn unreg(&self, idx: u32) {
if idx == 0 {
self.readiness.unregister();
}
self.waiters.remove(idx);
}
fn ready<'a>(&'a self, idx: u32, st: &'a St) -> BoxFuture<'a, Result<(), S::Error>>
where
Req: 'a,
{
Box::pin(self.readiness.ready(move || async move {
Ctx::<'_, S, St>::new(idx, &self.waiters, st)
.ready_generation(&self.s)
.await
}))
}
fn shutdown<'a>(&'a self, idx: u32, st: &'a St) -> BoxFuture<'a, ()> {
Box::pin(async move {
self.waiters.shutdown();
self.readiness.cancel();
Ctx::<'_, S, St>::new(idx, &self.waiters, st)
.shutdown(&self.s)
.await;
})
}
fn call<'a>(&'a self, idx: u32, req: Req, st: &'a St) -> BoxFuture<'a, Result<S::Res, S::Error>>
where
Req: 'a,
{
Box::pin(async move {
let ctx = Ctx::<'_, S, St>::new(idx, &self.waiters, st);
self.readiness
.ready(|| ctx.ready_generation(&self.s))
.await?;
self.s.call(req, ctx).await
})
}
fn poll_ready(&self, cx: &mut Context<'_>, st: &St) -> Poll<Result<(), S::Error>>
where
St: Clone,
{
assert!(!self.waiters.is_shutdown(), "Pipeline is shutting down");
self.readiness.poll_ready(cx, || {
// SAFETY: `self` is heap allocated (`Rc<PipelineInner>`) and never moves.
// `fut` is stored in `self.readiness`, which is reset before other
// fields are dropped (see `Drop for PipelineInner`), so `pl` outlives `fut`.
let pl = unsafe { &*(ptr::from_ref(self)) };
let st = st.clone();
Box::pin(async move {
let registration = pl.waiters.register();
Ctx::<'_, S, St>::new(registration.idx, &pl.waiters, &st)
.ready_generation(&pl.s)
.await
})
})
}
fn is_shutdown(&self) -> bool {
self.waiters.is_shutdown()
}
}
impl<St, Req, Res, Err> fmt::Debug for PipelineState<St, Req, Res, Err> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PipelineState").finish()
}
}
impl<St, Req, Res, Err> fmt::Debug for PipelineStateBinding<St, Req, Res, Err> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PipelineStateBinding").finish()
}
}
#[cfg(test)]
#[path = "../tests/unit/pl_state.rs"]
mod tests;