ntex-util 4.5.1

Utilities for ntex framework
Documentation
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
use std::{cell::Cell, convert::Infallible, fmt, rc::Rc, rc::Weak, time};

use ntex_service::{BindCtx, Ctx, ReadyNotifier, Service, ServiceFactory};

use crate::time::{CallbackTimer, Millis, now};

/// Middleware that fails readiness once the service has been idle too long.
///
/// Every call records the current time and passes the request through
/// unchanged. When no call has been made for the keep-alive duration,
/// [`ready`](Service::ready) fails with the error produced by `f`.
/// A timer callback invalidates cached readiness on expiration, even when
/// readiness is not being polled. Extended deadlines are rearmed without
/// notifying readiness; active calls only update the activity timestamp.
pub struct KeepAlive<F, E>
where
    F: Fn() -> E + Clone,
{
    f: F,
    ka: Millis,
}

impl<F, E> KeepAlive<F, E>
where
    F: Fn() -> E + Clone,
{
    /// Creates keep-alive middleware.
    ///
    /// `ka` is the maximum idle time between calls, and `f` creates the error
    /// returned once it is exceeded.
    pub fn new(ka: Millis, f: F) -> Self {
        KeepAlive { f, ka }
    }
}

impl<F, E> Clone for KeepAlive<F, E>
where
    F: Fn() -> E + Clone,
{
    fn clone(&self) -> Self {
        KeepAlive {
            f: self.f.clone(),
            ka: self.ka,
        }
    }
}

impl<F, E> fmt::Debug for KeepAlive<F, E>
where
    F: Fn() -> E + Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KeepAlive")
            .field("ka", &self.ka)
            .field("f", &std::any::type_name::<F>())
            .finish()
    }
}

impl<F, E, St, Req> ServiceFactory<St, Req> for KeepAlive<F, E>
where
    F: Fn() -> E + Clone,
{
    type Res = Req;
    type Error = E;

    type Service = KeepAliveService<F, E>;
    type InitError = Infallible;

    #[inline]
    async fn create(&self, _: &St) -> Result<Self::Service, Self::InitError> {
        Ok(KeepAliveService::new(self.ka, self.f.clone()))
    }
}

/// A service that fails readiness once it has been idle too long.
///
/// See [`KeepAlive`] for details.
pub struct KeepAliveService<F, E>
where
    F: Fn() -> E,
{
    f: F,
    shared: Rc<Shared>,
}

struct Shared {
    dur: time::Duration,
    timer: CallbackTimer,
    expire: Cell<time::Instant>,
    expired: Cell<bool>,
    stopped: Cell<bool>,
    changed: ReadyNotifier,
}

impl Shared {
    fn on_timeout(&self) {
        let now = now();
        let expire = self.expire.get() + self.dur;
        if expire <= now {
            self.expired.set(true);
            self.changed.notify();
        } else {
            let millis: u32 = (expire - now).as_millis().try_into().unwrap_or(u32::MAX);
            self.timer.reset(Millis(millis.max(1)));
        }
    }
}

impl<F, E> KeepAliveService<F, E>
where
    F: Fn() -> E,
{
    /// Creates a keep-alive service with the maximum idle time `dur`.
    ///
    /// `f` creates the error returned once the service has been idle longer
    /// than `dur`. The callback timer is canceled on shutdown or drop.
    pub fn new(dur: Millis, f: F) -> Self {
        KeepAliveService {
            f,
            shared: Rc::new_cyclic(|weak: &Weak<Shared>| {
                let weak = weak.clone();
                Shared {
                    dur: time::Duration::from(dur),
                    expire: Cell::new(now()),
                    expired: Cell::new(false),
                    stopped: Cell::new(false),
                    changed: ReadyNotifier::new(),
                    timer: CallbackTimer::new(dur, move || {
                        if let Some(shared) = weak.upgrade() {
                            shared.on_timeout();
                        }
                    }),
                }
            }),
        }
    }
}

impl<F, E> fmt::Debug for KeepAliveService<F, E>
where
    F: Fn() -> E,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KeepAliveService")
            .field("dur", &self.shared.dur)
            .field("expire", &self.shared.expire)
            .field("f", &std::any::type_name::<F>())
            .finish()
    }
}

impl<F, E, St, Req> Service<St, Req> for KeepAliveService<F, E>
where
    F: Fn() -> E,
{
    type Res = Req;
    type Error = E;

    fn bind(&self, ctx: BindCtx<'_>) {
        ctx.subscribe(&self.shared.changed);
    }

    async fn ready(&self, _: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
        if self.shared.expire.get() + self.shared.dur <= now() {
            Err((self.f)())
        } else {
            Ok(())
        }
    }

    #[inline]
    async fn call(&self, req: Req, _: Ctx<'_, Self, St>) -> Result<Req, E> {
        let shared = &self.shared;
        let now = now();
        let previous = shared.expire.replace(now);
        let expired = shared.expired.replace(false);
        if expired || previous + shared.dur <= now {
            if !shared.stopped.get() {
                shared.timer.reset(shared.dur.into());
            }
            shared.changed.notify();
        }
        Ok(req)
    }

    async fn shutdown(&self, _: Ctx<'_, Self, St>) {
        self.shared.stopped.set(true);
        self.shared.timer.cancel();
    }
}

#[cfg(test)]
mod tests {
    use std::{pin::Pin, task::Context, task::Poll, task::ready};

    use ntex_service::{Pipeline, ReadyCallback, boxed, factory};

    use super::*;
    use crate::{channel::oneshot, spawn, time::sleep};

    #[derive(Debug, PartialEq)]
    struct TestErr;

    struct Dispatcher {
        p: Pipeline<usize, usize, TestErr>,
        tx: Option<oneshot::Sender<()>>,
    }

    impl Future for Dispatcher {
        type Output = ();

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let mut this = self.as_mut();
            if ready!(this.p.poll_ready(cx)).is_err() {
                if let Some(tx) = this.tx.take() {
                    let _ = tx.send(());
                }
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        }
    }

    /// STATEMENT: Keep-alive calls pass through until inactivity causes a readiness error.
    ///
    /// HOW:
    /// - Create and inspect a keep-alive factory and service, then make spaced calls.
    /// - Wait for the readiness dispatcher to report expiry and verify the error.
    #[ntex::test]
    async fn test_ka() {
        // the keep-alive is much longer than the sleep between calls, so a
        // delayed wakeup does not expire it
        let factory = factory::<_, (), usize>(KeepAlive::new(Millis(500), || TestErr));
        assert!(format!("{factory:?}").contains("KeepAlive"));
        let _ = factory.clone();

        let svc = factory.create(&()).await.unwrap();
        assert!(format!("{svc:?}").contains("KeepAliveService"));

        let p = Pipeline::new((), boxed::service(svc));
        assert_eq!(p.call(1usize).await, Ok(1usize));
        let svc = p.bind();

        let (tx, rx) = oneshot::channel();
        drop(spawn(Dispatcher { p, tx: Some(tx) }));

        sleep(Millis(25)).await;
        assert_eq!(svc.call(1usize).await, Ok(1usize));
        sleep(Millis(100)).await;

        let res = rx.await;
        assert_eq!(res, Ok(()));
        assert_eq!(svc.ready().await, Err(TestErr));
    }

    /// STATEMENT: A timeout callback does not expire a keep-alive with submillisecond time left.
    ///
    /// HOW:
    /// - Set the last activity so half a millisecond remains and invoke the callback.
    /// - Verify readiness succeeds, the expired flag stays clear, and the timer is armed.
    #[ntex::test]
    async fn test_ka_sub_millis() {
        let svc = Rc::new(KeepAliveService::new(Millis(100), || TestErr));
        let p = Pipeline::<usize, usize, TestErr>::new((), svc.clone()).bind();
        svc.shared.timer.cancel();

        // less than millisecond is left before expiration
        svc.shared
            .expire
            .set(now().checked_sub(svc.shared.dur).unwrap() + time::Duration::from_micros(500));
        svc.shared.on_timeout();
        assert_eq!(p.ready().await, Ok(()));
        assert!(!svc.shared.expired.get());
        assert!(!svc.shared.timer.is_elapsed());
    }

    fn notification(p: &Pipeline<usize, usize, TestErr>) -> oneshot::Receiver<()> {
        let (tx, rx) = oneshot::channel();
        let tx = Cell::new(Some(tx));
        p.on_notify(&ReadyCallback::new(move |_| {
            if let Some(tx) = tx.take() {
                let _ = tx.send(());
            }
        }));
        rx
    }

    /// STATEMENT: Idle expiry invalidates cached readiness for every pipeline sharing a service.
    ///
    /// HOW:
    /// - Prime two pipelines' readiness and wait for an expiry notification.
    /// - Check both caches are invalidated and both readiness and a subsequent call fail.
    #[ntex::test]
    async fn idle_timer_invalidates_completed_readiness_for_all_roots() {
        let svc = Rc::new(KeepAliveService::new(Millis(25), || TestErr));
        let first = Pipeline::new((), svc.clone());
        let second = Pipeline::<usize, usize, TestErr>::new((), svc);
        assert_eq!(first.ready().await, Ok(()));
        assert_eq!(second.ready().await, Ok(()));
        let expired = notification(&first);

        crate::time::timeout(Millis(1_000), expired)
            .await
            .unwrap()
            .unwrap();
        assert!(!first.is_ready());
        assert!(!second.is_ready());
        assert_eq!(first.ready().await, Err(TestErr));
        assert_eq!(second.ready().await, Err(TestErr));
        assert_eq!(first.call(1).await, Err(TestErr));
    }

    /// STATEMENT: A call extends the deadline before an overdue timer callback is processed.
    ///
    /// HOW:
    /// - Force the old timer due, make a call, and verify no premature expiry or notification.
    /// - Await the extended deadline and check one notification and subsequent failures.
    #[ntex::test]
    async fn call_extends_deadline_before_old_timer_is_processed() {
        let svc = Rc::new(KeepAliveService::new(Millis(250), || TestErr));
        let p = Pipeline::new((), svc.clone());
        let notifications = Rc::new(Cell::new(0));
        let count = notifications.clone();
        p.on_notify(&ReadyCallback::new(move |_| count.set(count.get() + 1)));
        assert_eq!(p.ready().await, Ok(()));
        svc.shared.timer.reset(Millis::ZERO);
        assert_eq!(p.call(1).await, Ok(1));
        sleep(Millis(50)).await;
        assert!(p.is_ready());
        assert_eq!(notifications.get(), 0);
        assert!(!svc.shared.expired.get());
        assert!(!svc.shared.timer.is_elapsed());

        crate::time::timeout(Millis(1_000), notification(&p))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(notifications.get(), 1);
        assert_eq!(p.ready().await, Err(TestErr));
        assert_eq!(p.call(2).await, Err(TestErr));
    }

    struct Bypass<S>(S);

    impl<S: Service<(), usize, Res = usize, Error = TestErr>> Service<(), usize> for Bypass<S> {
        type Res = usize;
        type Error = TestErr;

        ntex_service::forward_bind!((), 0);

        #[allow(
            deprecated,
            reason = "verify reset after an intentional readiness bypass"
        )]
        async fn call(&self, req: usize, ctx: Ctx<'_, Self>) -> Result<usize, TestErr> {
            ctx.call_nowait(&self.0, req).await
        }
    }

    /// STATEMENT: An intentionally bypassed call rearms an expired keep-alive timer.
    ///
    /// HOW:
    /// - Wait for expiry, then call through a wrapper that bypasses readiness.
    /// - Check readiness recovers and the rearmed timer later expires again.
    #[ntex::test]
    async fn bypassed_call_rearms_an_expired_timer() {
        let svc = Rc::new(KeepAliveService::new(Millis(25), || TestErr));
        let p = Pipeline::new((), svc.clone());
        let bypass = Pipeline::new((), Bypass(svc.clone()));
        p.ready().await.unwrap();
        crate::time::timeout(Millis(1_000), notification(&p))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(p.ready().await, Err(TestErr));

        assert_eq!(bypass.call(1).await, Ok(1));
        assert!(!svc.shared.timer.is_elapsed());
        assert_eq!(p.ready().await, Ok(()));
        crate::time::timeout(Millis(1_000), notification(&p))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(p.ready().await, Err(TestErr));
    }

    /// STATEMENT: Shutdown and drop cancel timer callbacks without retaining keep-alive state.
    ///
    /// HOW:
    /// - Exercise drop alone and shutdown followed by drop while counting notifications.
    /// - Verify no delayed notifications, no rearming after shutdown, and release of shared state.
    #[ntex::test]
    async fn timer_callback_is_canceled_on_shutdown_and_drop() {
        for shutdown in [false, true] {
            let svc = Rc::new(KeepAliveService::new(Millis(25), || TestErr));
            let state = Rc::downgrade(&svc.shared);
            let p = Pipeline::<usize, usize, TestErr>::new((), svc.clone());
            let notifications = Rc::new(Cell::new(0));
            let count = notifications.clone();
            p.on_notify(&ReadyCallback::new(move |_| count.set(count.get() + 1)));
            p.ready().await.unwrap();
            if shutdown {
                p.shutdown().await;
                assert!(svc.shared.timer.is_elapsed());
                sleep(Millis(75)).await;
                assert_eq!(notifications.get(), 0);

                let bypass = Pipeline::new((), Bypass(svc.clone()));
                assert_eq!(bypass.call(1).await, Ok(1));
                assert!(svc.shared.timer.is_elapsed());
                let before = notifications.get();
                sleep(Millis(75)).await;
                assert_eq!(notifications.get(), before);
            }
            let before = notifications.get();
            drop(p);
            drop(svc);
            assert!(state.upgrade().is_none());
            sleep(Millis(75)).await;
            assert_eq!(notifications.get(), before);
        }
    }

    /// STATEMENT: A zero keep-alive duration rejects readiness and calls immediately.
    ///
    /// HOW:
    /// - Construct a zero-duration service and assert its configured error from both operations.
    #[ntex::test]
    async fn test_ka_zero() {
        let p = Pipeline::<usize, usize, TestErr>::new(
            (),
            KeepAliveService::new(Millis::ZERO, || TestErr),
        );
        assert_eq!(p.ready().await, Err(TestErr));
        assert_eq!(p.call(1).await, Err(TestErr));
    }
}