ntex-io 4.3.0

Utilities for abstracting io streams
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#![allow(clippy::cast_possible_truncation)]
use std::collections::{BTreeMap, VecDeque};
use std::{cell::RefCell, mem, num::NonZeroUsize, ops, rc::Rc, time::Duration, time::Instant};

use ntex_rt::Arbiter;
use ntex_util::time::{Millis, Seconds, now, sleep};
use ntex_util::{HashSet, spawn};
use slab::Slab;

use crate::IoRef;

const CAP: usize = 32;
/// Timer sets with a larger capacity are dropped instead of cached.
const MAX_CACHED_SET_CAP: usize = 512;

thread_local! {
    static MANAGER: RefCell<Option<IoManager>> = const { RefCell::new(None) };
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
/// Opaque identifier assigned to a registered I/O stream.
///
/// The identifier is meaningful only within the current thread's I/O manager.
pub struct Id(Option<NonZeroUsize>);

#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Handle to an I/O dispatcher timer.
///
/// Handles represent second-granularity deadlines managed by the current
/// thread's I/O manager, a timer started with a `t` seconds timeout expires
/// after at least `t` and less than `t + 1` seconds. The timer clock starts
/// a new second when a timer starts while no other timers are pending, such
/// a timer expires after `t` seconds. A handle becomes stale
/// after its timer is stopped or replaced and must not be used as an
/// independent cancellation token.
pub struct TimerHandle(u32);

impl TimerHandle {
    /// A handle that does not refer to an active timer.
    pub const ZERO: TimerHandle = TimerHandle(0);

    /// Returns `true` if this handle refers to a timer deadline.
    ///
    /// This does not guarantee that the timer is still registered or has not
    /// already elapsed.
    pub fn is_set(&self) -> bool {
        self.0 != 0
    }

    /// Returns the whole seconds remaining until this handle's deadline.
    ///
    /// Returns zero if the deadline has elapsed. The remaining time is
    /// measured from the cached [`now()`](ntex_util::time::now), which lags
    /// the clock, and is rounded up.
    pub fn remains(&self) -> Seconds {
        let rem = self.instant().saturating_duration_since(now());
        let secs = rem.as_secs() + u64::from(rem.subsec_nanos() != 0);
        Seconds(secs.min(u64::from(u16::MAX)) as u16)
    }

    /// Returns the instant represented by this handle.
    ///
    /// The instant is based on the current thread's I/O timer clock. For
    /// [`ZERO`](Self::ZERO), this is the clock's base instant.
    pub fn instant(&self) -> Instant {
        IoManager::with(|mgr| mgr.timers.base + Duration::from_secs(u64::from(self.0)))
    }

    pub(crate) fn update(self, timeout: Seconds, io: &IoRef) -> TimerHandle {
        IoManager::with(|mgr| {
            let new_hnd = mgr.timers.deadline(timeout);
            if self.0 == new_hnd || self.0 == new_hnd + 1 {
                self
            } else {
                mgr.timers.unregister(self, io);
                mgr.timers.register(timeout, io)
            }
        })
    }

    pub(crate) fn unregister(self, io: &IoRef) {
        IoManager::with(|manager| manager.timers.unregister(self, io));
    }

    pub(crate) fn register(timeout: Seconds, io: &IoRef) -> TimerHandle {
        IoManager::with(move |mgr| mgr.timers.register(timeout, io))
    }
}

impl ops::Add<Seconds> for TimerHandle {
    type Output = TimerHandle;

    #[inline]
    fn add(self, other: Seconds) -> TimerHandle {
        TimerHandle(self.0 + u32::from(other.0))
    }
}

struct TimerStorage {
    running: bool,
    base: Instant,
    /// Whole seconds elapsed since `base`, the keys up to it have expired.
    current: u32,
    cache: VecDeque<HashSet<Id>>,
    notifications: BTreeMap<u32, HashSet<Id>>,
}

impl TimerStorage {
    fn unregister(&mut self, hnd: TimerHandle, io: &IoRef) {
        if let Some(items) = self.notifications.get_mut(&hnd.0) {
            items.remove(&io.id());
            if items.is_empty() {
                // the timer stops once no timers are left
                let items = self.notifications.remove(&hnd.0).unwrap();
                self.recycle(items);
            }
        }
    }

    /// Returns an empty timer set to the cache, a set grown by a burst of
    /// timers is dropped so its capacity is not retained.
    fn recycle(&mut self, items: HashSet<Id>) {
        debug_assert!(items.is_empty());
        if self.cache.len() < CAP && items.capacity() <= MAX_CACHED_SET_CAP {
            self.cache.push_back(items);
        }
    }

    /// Updates `current` from the clock.
    fn update_current(&mut self) -> u32 {
        self.current = self.base.elapsed().as_secs() as u32;
        self.current
    }

    /// Returns the key of a timer started now, it expires after at least
    /// `timeout` and less than `timeout + 1` seconds.
    ///
    /// The start is the cached [`now()`](ntex_util::time::now), a timer
    /// expires early by the age of the cached time.
    fn deadline(&mut self, timeout: Seconds) -> u32 {
        let elapsed = now().saturating_duration_since(self.base);
        let secs = elapsed.as_secs() as u32;
        if secs < self.current {
            // the cached time lags the ticker, keys up to `current` expired
            return self.current + 1 + u32::from(timeout.0);
        }
        self.current = secs;

        if self.notifications.is_empty() {
            // no timers are pending, start the clock second now so a timer
            // expires after exactly `timeout`. `current` does not change,
            // stale handles cannot match a new key
            self.base += Duration::new(0, elapsed.subsec_nanos());
            self.current + u32::from(timeout.0)
        } else {
            let partial = u32::from(elapsed.subsec_nanos() != 0);
            self.current + partial + u32::from(timeout.0)
        }
    }

    fn register(&mut self, timeout: Seconds, io: &IoRef) -> TimerHandle {
        let hnd = self.deadline(timeout);
        if let Some(items) = self.notifications.get_mut(&hnd) {
            items.insert(io.id());
        } else {
            let mut items = self.cache.pop_front().unwrap_or_default();
            items.insert(io.id());
            self.notifications.insert(hnd, items);
        }

        self.run_timer();

        TimerHandle(hnd)
    }

    fn run_timer(&mut self) {
        if self.running {
            return;
        }
        self.running = true;

        spawn(async move {
            let guard = TimerGuard;
            // one wheel timer is reset for every tick
            let timer = sleep(Self::next_tick());
            loop {
                timer.wait().await;

                let stop = IoManager::with(|mgr| {
                    let current = mgr.timers.update_current();

                    // notify io dispatchers of all expired timers
                    while let Some(entry) = mgr.timers.notifications.first_entry() {
                        if *entry.key() > current {
                            break;
                        }
                        let mut items = entry.remove();
                        for id in items.drain() {
                            if let Some(io) = mgr.get(id) {
                                io.notify_timeout();
                            }
                        }
                        mgr.timers.recycle(items);
                    }

                    if mgr.timers.notifications.is_empty() {
                        mgr.timers.running = false;
                        true
                    } else {
                        false
                    }
                });

                if stop {
                    break;
                }
                timer.reset(Self::next_tick());
            }
            drop(guard);
        });
    }

    /// Returns the delay until the next whole second of the timer clock, a
    /// late tick does not delay the later ones.
    fn next_tick() -> Millis {
        let next = IoManager::with(|mgr| {
            let t = &mgr.timers;
            let next = t.base + Duration::from_secs(u64::from(t.current) + 1);
            next.saturating_duration_since(Instant::now())
        });
        Millis(next.as_millis() as u32 + 1)
    }
}

struct TimerGuard;

impl Drop for TimerGuard {
    fn drop(&mut self) {
        IoManager::with(|mgr| {
            mgr.timers.running = false;
            mgr.timers.notifications.clear();
        });
    }
}

pub(crate) struct IoManager {
    storage: Slab<Option<IoRef>>,
    timers: TimerStorage,
    pub(crate) iops: Iops,
}

impl Default for IoManager {
    fn default() -> IoManager {
        let mut storage = Slab::new();
        assert_eq!(storage.insert(None), 0);

        IoManager {
            storage,
            timers: TimerStorage {
                running: false,
                base: Instant::now(),
                current: 0,
                cache: VecDeque::with_capacity(CAP),
                notifications: BTreeMap::default(),
            },
            iops: Iops {
                running: false,
                ops: Vec::with_capacity(32),
            },
        }
    }
}

impl IoManager {
    /// Calls `f` with the current thread's manager.
    ///
    /// The manager is created on first use and dropped when the arbiter shuts
    /// down, so that its state does not carry over to the next runtime on the
    /// same thread. If the thread-local storage has already been destroyed
    /// because the thread is exiting, `f` receives a temporary manager.
    fn with<F, R>(f: F) -> R
    where
        F: FnOnce(&mut IoManager) -> R,
    {
        let mut f = Some(f);
        MANAGER
            .try_with(|cell| {
                let mut mgr = cell.borrow_mut();
                let mgr = mgr.get_or_insert_with(|| {
                    Arbiter::on_shutdown(IoManager::reset);
                    IoManager::default()
                });
                (f.take().unwrap())(mgr)
            })
            .unwrap_or_else(|_| (f.take().unwrap())(&mut IoManager::default()))
    }

    fn reset() {
        // dropped outside of the borrow, the registered streams it holds may
        // unregister themselves
        let mgr = MANAGER
            .try_with(|cell| cell.borrow_mut().take())
            .ok()
            .flatten();
        drop(mgr);
    }

    fn get(&self, id: Id) -> Option<&IoRef> {
        if let Some(id) = id.0 {
            self.storage.get(id.get()).and_then(|item| item.as_ref())
        } else {
            None
        }
    }

    pub(crate) fn register(io: &IoRef) -> Id {
        IoManager::with(|manager| {
            let entry = manager.storage.vacant_entry();
            let id = Id(NonZeroUsize::new(entry.key()));
            entry.insert(Some(io.clone()));
            id
        })
    }

    pub(crate) fn unregister(io: &IoRef) {
        if let Some(id) = io.id().0 {
            io.0.id.set(Id(None));
            IoManager::with(|manager| {
                // the manager may have been reset since the stream registered,
                // the id can belong to another stream then
                if let Some(Some(item)) = manager.storage.get(id.get())
                    && Rc::ptr_eq(&item.0, &io.0)
                {
                    manager.storage.remove(id.get());
                }
            });
        }
    }
}

pub(crate) struct Iops {
    running: bool,
    pub(crate) ops: Vec<Id>,
}

impl Iops {
    pub(crate) fn schedule_write(id: Id) {
        IoManager::with(|mgr| {
            mgr.iops.ops.push(id);

            if !mgr.iops.running {
                mgr.iops.running = true;
                spawn(async move { Iops::run() });
            }
        });
    }

    pub(crate) fn run() {
        IoManager::with(|mgr| {
            mgr.iops.running = false;

            let mut ops = mem::take(&mut mgr.iops.ops);
            for id in ops.drain(..) {
                if let Some(io) = mgr.get(id) {
                    io.ops_send_buf();
                }
            }
            let _ = mem::replace(&mut mgr.iops.ops, ops);
        });
    }

    #[cfg(test)]
    pub(crate) fn is_registered(io: &IoRef) -> bool {
        IoManager::with(|mgr| mgr.iops.ops.contains(&io.id()))
    }
}

#[cfg(test)]
mod tests {
    use ntex::rt::{DefaultRuntime, System};

    use super::*;

    async fn wait_timeout(io: &crate::Io) -> Duration {
        let start = Instant::now();
        let st = std::future::poll_fn(|cx| io.poll_status_update(cx)).await;
        assert!(matches!(st, crate::IoStatusUpdate::Timeout));
        start.elapsed()
    }

    #[test]
    fn large_timer_sets_are_not_cached() {
        let mut timers = TimerStorage {
            running: false,
            base: Instant::now(),
            current: 0,
            cache: VecDeque::new(),
            notifications: BTreeMap::new(),
        };

        let mut small = HashSet::default();
        small.insert(Id(NonZeroUsize::new(1)));
        small.clear();
        timers.recycle(small);
        assert_eq!(timers.cache.len(), 1);

        // a set grown by a burst of timers is dropped
        let mut large = HashSet::default();
        for i in 1..=10_000 {
            large.insert(Id(NonZeroUsize::new(i)));
        }
        large.clear();
        timers.recycle(large);
        assert_eq!(timers.cache.len(), 1);
        assert!(timers.cache[0].capacity() <= MAX_CACHED_SET_CAP);

        // the cache holds at most `CAP` sets
        for _ in 0..2 * CAP {
            timers.recycle(HashSet::default());
        }
        assert_eq!(timers.cache.len(), CAP);
    }

    /// A timer started while no timers are pending expires after its
    /// timeout, the ticker does not add a second.
    #[ntex::test]
    async fn timer_expires_after_timeout() {
        use ntex_service::cfg::SharedCfg;

        use crate::{Io, testing::IoTest};

        let (_client, server) = IoTest::create();
        let io = Io::new(server, SharedCfg::new("T"));

        // the clock is not aligned to the start of the runtime
        sleep(Millis(500)).await;

        io.start_timer(Seconds(1));
        let elapsed = wait_timeout(&io).await;
        assert!(
            elapsed >= Duration::from_secs(1) && elapsed < Duration::from_millis(1300),
            "elapsed: {elapsed:?}"
        );
    }

    /// A timer started while other timers are pending expires within a
    /// second after its timeout.
    #[ntex::test]
    async fn timer_expires_within_second_after_timeout() {
        use ntex_service::cfg::SharedCfg;

        use crate::{Io, testing::IoTest};

        let (_client1, server1) = IoTest::create();
        let (_client2, server2) = IoTest::create();
        let io1 = Io::new(server1, SharedCfg::new("T"));
        let io2 = Io::new(server2, SharedCfg::new("T"));

        io1.start_timer(Seconds(10));
        sleep(Millis(500)).await;

        // expires at the second clock second
        io2.start_timer(Seconds(1));
        let elapsed = wait_timeout(&io2).await;
        assert!(
            elapsed >= Duration::from_secs(1) && elapsed < Duration::from_millis(1800),
            "elapsed: {elapsed:?}"
        );
    }

    fn has_manager() -> bool {
        MANAGER.with(|mgr| mgr.borrow().is_some())
    }

    /// The manager must not carry over to the next runtime on the thread, a
    /// write scheduled by a runtime that has stopped would block the writes of
    /// the next one.
    #[test]
    fn manager_reset_on_shutdown() {
        std::thread::spawn(|| {
            System::new("test", DefaultRuntime).block_on(async {
                Iops::schedule_write(Id(None));
                assert!(has_manager());
            });
            assert!(!has_manager());

            System::build()
                .build(DefaultRuntime)
                .run(|| {
                    Iops::schedule_write(Id(None));
                    System::current().stop();
                    Ok(())
                })
                .unwrap();
            assert!(!has_manager());
        })
        .join()
        .unwrap();
    }
}