wheeltimer 0.2.0

wheelTimer is Netty's HashedWheelTimer implementation based on Rust
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! # wheeltimer
//!
//! An implementation of the time wheel algorithm in Rust, inspired by Netty's `HashedWheelTimer`.
//! Provides functionality for creating and cancelling delayed tasks in an asynchronous environment.
//!
//! ## Features
//! - Asynchronous task scheduling with configurable tick duration and wheel size
//! - Support for cancellation of pending timeouts
//! - Thread-safe design using `Arc`, `RwLock`, and `Mutex`


use std::cmp::max;
use std::collections::VecDeque;
use std::fmt::{Display, Formatter};
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicU8, AtomicUsize};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::{Mutex, Notify, RwLock, RwLockWriteGuard};

const WORKER_STATE_INIT: u8 = 0;
const WORKER_STATE_STARTED: u8 = 1;
const WORKER_STATE_SHUTDOWN: u8 = 2;

/// Enumeration of possible errors returned by timer operations
#[derive(Debug)]
pub enum Error {
    /// Ticks per wheel must be greater than zero
    TicksPerWheelError(u32),

    /// Number of pending timeouts exceeds allowed maximum
    MaxPendingTimeoutCountError(u64, u64),

    /// Generic unknown error
    UnknownError,
}


impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::TicksPerWheelError(ticks_per_wheel) => {
                write!(f, "ticksPerWheel must be greater than 0: {}", ticks_per_wheel)
            }
            Error::MaxPendingTimeoutCountError(pending_timeouts, max_pending_timeouts) => {
                write!(f, "Number of pending timeouts ({}) is greater than or equal to maximum allowed pending timeouts ({})", pending_timeouts, max_pending_timeouts)
            }
            Error::UnknownError => {
                write!(f, "unknown error")
            }
        }
    }
}

/// A reference-counted handle to a time wheel-based timer.
///
/// # Examples
/// ```
/// use wheeltimer::WheelTimer;
///
/// #[tokio::main]
/// async fn main() {
///     let timer = WheelTimer::new(tokio::time::Duration::from_millis(100), 1024, 100000).unwrap();
///     timer.start().await;
///     let timeout = timer.new_timeout(tokio::time::Duration::from_secs(2),
///         async move {
///             // async task logic here
///             println!("a delay task is executed");
///         }
///     ).await;
///
///     tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
///     timer.stop().await;
/// }
///
/// ```
#[derive(Clone)]
pub struct WheelTimer {
    inner: Arc<WheelTimerInner>,
}

struct WheelTimerInner {
    worker_state: AtomicU8,
    tick_duration: u64,
    wheel: Vec<WheelBucket>,
    mask: u32,
    timeout_queue: Mutex<VecDeque<Timeout>>,
    cancelled_queue: Mutex<VecDeque<Timeout>>,
    pending_timeouts: AtomicU64,
    max_pending_timeouts: u64,
    start_time: RwLock<u64>,
}

impl WheelTimer {
    pub fn new(tick_duration: Duration, ticks_per_wheel: u32, max_pending_timeouts: u64) -> Result<Self, Error> {
        if ticks_per_wheel == 0 {
            return Err(Error::TicksPerWheelError(ticks_per_wheel));
        }
        let mut temp = 1;
        while temp < ticks_per_wheel {
            temp <<= 1;
        }
        let ticks_per_wheel = temp;
        let wheel: Vec<WheelBucket> = (0..temp).map(|_| {
            WheelBucket::new()
        }).collect();
        let mask = ticks_per_wheel - 1;
        Ok(Self {
            inner: Arc::new(WheelTimerInner {
                worker_state: AtomicU8::new(WORKER_STATE_INIT),
                tick_duration: tick_duration.as_nanos() as u64,
                wheel,
                mask,
                timeout_queue: Mutex::new(VecDeque::new()),
                cancelled_queue: Mutex::new(VecDeque::new()),
                pending_timeouts: AtomicU64::new(0),
                max_pending_timeouts,
                start_time: RwLock::new(0),
            })
        })
    }

    pub async fn start(&self) {
        if let Ok(_) = self.inner.worker_state.compare_exchange(WORKER_STATE_INIT, WORKER_STATE_STARTED, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst) {
            let self_clone = self.clone();
            let count_down_latch = CountDownLatch::new(1);
            let cdl_clone = count_down_latch.clone();
            tokio::spawn(async move {
                let mut worker = Worker::new(self_clone);
                worker.run(&cdl_clone).await;
            });
            count_down_latch.wait().await;
        }
    }

    pub async fn stop(&self) {
        let _ = self.inner.worker_state.compare_exchange(WORKER_STATE_STARTED, WORKER_STATE_SHUTDOWN, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst);
    }

    pub async fn new_timeout<F>(&self, delay: Duration, task: F) -> Result<Timeout, Error>
    where
        F: Future<Output=()> + Send + 'static,
    {
        let current_pending_timeouts = self.inner.pending_timeouts.fetch_add(1, std::sync::atomic::Ordering::SeqCst);

        if self.inner.max_pending_timeouts > 0 && current_pending_timeouts > self.inner.max_pending_timeouts {
            self.inner.pending_timeouts.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
            return Err(Error::MaxPendingTimeoutCountError(current_pending_timeouts, self.inner.max_pending_timeouts));
        }

        self.start().await;

        let deadline = {
            let start_time = *self.inner.start_time.read().await;
            system_time_nanos() + delay.as_nanos() as u64 - start_time
        };

        let timeout = Timeout::new(self.clone(), deadline, Box::pin(task));
        self.inner.timeout_queue.lock().await.push_back(timeout.clone());

        Ok(timeout)
    }

    pub fn pending_timeouts(&self) -> u64 {
        self.inner.pending_timeouts.load(std::sync::atomic::Ordering::SeqCst)
    }
}

fn system_time_nanos() -> u64 {
    let since_the_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
    since_the_epoch.as_secs() * 1_000_000_000 + since_the_epoch.subsec_nanos() as u64
}

struct Worker {
    timer: WheelTimer,
    tick: u64,
    tick_duration: u64,
    wheel_length: u64,
    unprocessed_timeouts: Vec<Timeout>,
}

impl Worker {
    fn new(timer: WheelTimer) -> Self {
        Worker {
            tick_duration: timer.inner.tick_duration,
            wheel_length: timer.inner.wheel.len() as u64,
            timer,
            tick: 0,
            unprocessed_timeouts: Vec::new(),
        }
    }

    async fn run(&mut self, count_down_latch: &CountDownLatch) {
        {
            let mut start_time = self.timer.inner.start_time.write().await;
            *start_time = system_time_nanos();

            count_down_latch.count_down();
        }
        let mut interval = tokio::time::interval(Duration::from_nanos(self.tick_duration));
        loop {
            if self.timer.inner.worker_state.load(std::sync::atomic::Ordering::SeqCst) == WORKER_STATE_STARTED {
                interval.tick().await;
                if self.timer.inner.worker_state.load(std::sync::atomic::Ordering::SeqCst) != WORKER_STATE_STARTED {
                    break;
                }
            } else {
                break;
            }
            let deadline = self.tick_duration * (self.tick + 1);
            let idx = self.tick & self.timer.inner.mask as u64;
            self.process_cancelled_tasks().await;
            let bucket = self.timer.inner.wheel.get(idx as usize).unwrap();
            self.transfer_timeouts_to_buckets().await;
            bucket.expire_timeouts(deadline).await;
            self.tick += 1;
            continue;
        }

        for bucket in self.timer.inner.wheel.iter() {
            bucket.clear_timeouts(&mut self.unprocessed_timeouts).await;
        }

        loop {
            let mut timeout_queue = self.timer.inner.timeout_queue.lock().await;
            if let Some(timeout) = timeout_queue.pop_front() {
                if !timeout.is_cancelled() {
                    self.unprocessed_timeouts.push(timeout);
                }
            } else {
                break;
            }
        }
        self.process_cancelled_tasks().await;
    }

    async fn process_cancelled_tasks(&self) {
        let mut cancelled_queue = self.timer.inner.cancelled_queue.lock().await;
        while let Some(timeout) = cancelled_queue.pop_front() {
            timeout.remove().await;
        }
    }

    async fn transfer_timeouts_to_buckets(&self) {
        let mut timeout_queue = self.timer.inner.timeout_queue.lock().await;
        for _i in 0..100000 {
            if let Some(timeout) = timeout_queue.pop_front() {
                if timeout.is_cancelled() {
                    continue;
                }

                let calculated = {
                    let mut timeout_inner = timeout.inner.write().await;
                    let calculated = timeout_inner.deadline / self.tick_duration;

                    timeout_inner.remaining_rounds = if calculated > self.tick {
                        (calculated - self.tick) / self.wheel_length
                    } else {
                        0
                    };
                    calculated
                };
                let ticks = max(calculated, self.tick);

                let stop_index = ticks & self.timer.inner.mask as u64;
                self.timer.inner.wheel[stop_index as usize].add_timeout(timeout).await;
            } else {
                break;
            }
        }
    }

    #[allow(dead_code)]
    fn unprocessed_timeouts(&self) -> &Vec<Timeout> {
        &self.unprocessed_timeouts
    }
}

#[derive(Clone)]
struct WheelBucket {
    inner: Arc<RwLock<WheelBucketInner>>,
}

struct WheelBucketInner {
    head: Option<Timeout>,
    tail: Option<Timeout>,
}

impl WheelBucket {
    fn new() -> Self {
        WheelBucket {
            inner: Arc::new(RwLock::new(WheelBucketInner {
                head: None,
                tail: None,
            })),
        }
    }

    // 只有 worker 线程调用
    async fn add_timeout(&self, timeout: Timeout) {
        let mut bucket = self.inner.write().await;
        if bucket.head.is_none() {
            {
                let mut timeout_inner = timeout.inner.write().await;
                assert!(timeout_inner.bucket.is_none());
                timeout_inner.bucket = Some(self.clone());
            }
            bucket.head = Some(timeout);
            bucket.tail = bucket.head.clone();
        } else {
            let tail = bucket.tail.take().unwrap();
            {
                let mut timeout_inner = timeout.inner.write().await;
                assert!(timeout_inner.bucket.is_none());
                timeout_inner.bucket = Some(self.clone());
                timeout_inner.prev = Some(tail.clone());
            }
            let mut tail = tail.inner.write().await;
            tail.next = Some(timeout.clone());
            bucket.tail = Some(timeout);
        }
    }


    // worker 线程调用
    async fn expire_timeouts(&self, deadline: u64) {
        let mut mut_timeout = {
            let bucket = self.inner.read().await;
            bucket.head.clone()
        };
        while let Some(timeout) = mut_timeout {
            let mut timeout_inner = timeout.inner.write().await;
            if timeout_inner.remaining_rounds <= 0 {
                {
                    let mut bucket = self.inner.write().await;
                    timeout_inner.next = Self::remove(&mut bucket, &timeout, &mut timeout_inner).await;
                }
                if timeout_inner.deadline <= deadline {
                    timeout.expire().await;
                }
            } else if timeout.is_cancelled() {
                let mut bucket = self.inner.write().await;
                timeout_inner.next = Self::remove(&mut bucket, &timeout, &mut timeout_inner).await;
            } else {
                timeout_inner.remaining_rounds -= 1;
            }
            mut_timeout = timeout_inner.next.clone();
        }
    }

    // 多线程调用
    async fn remove(bucket: &mut RwLockWriteGuard<'_, WheelBucketInner>, timeout: &Timeout, timeout_inner: &mut RwLockWriteGuard<'_, WheelTimeoutInner>) -> Option<Timeout> {
        let next = timeout_inner.next.clone();
        if timeout_inner.prev.is_some() {
            let mut prev = timeout_inner.prev.as_ref().unwrap().inner.write().await;
            prev.next = next.clone();
        }
        if timeout_inner.next.is_some() {
            let mut next = timeout_inner.next.as_ref().unwrap().inner.write().await;
            next.prev = timeout_inner.prev.clone();
        }

        if let Some(head) = &bucket.head {
            if head == timeout {
                bucket.head = None;
                bucket.tail = None;
            } else {
                bucket.head = next.clone();
            }
        } else if let Some(tail) = &bucket.tail {
            if tail == timeout {
                bucket.tail = timeout_inner.prev.clone();
            }
        }
        timeout_inner.prev = None;
        timeout_inner.next = None;
        timeout_inner.bucket = None;

        timeout.timer.inner.pending_timeouts.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);

        next
    }

    // worker 线程调用
    async fn clear_timeouts(&self, timeouts: &mut Vec<Timeout>) {
        loop {
            if let Some(timeout) = self.poll_timeout().await {
                if timeout.is_expired() || timeout.is_cancelled() {
                    continue;
                }
                timeouts.push(timeout);
            } else {
                break;
            }
        }
    }

    async fn poll_timeout(&self) -> Option<Timeout> {
        let mut bucket = self.inner.write().await;
        let head_opt = bucket.head.take();
        if head_opt.is_none() {
            return None;
        }
        let head = head_opt.clone().unwrap();
        let mut head_inner = head.inner.write().await;
        if head_inner.next.is_none() {
            bucket.tail = None;
        } else {
            bucket.head = head_inner.next.clone();
            {
                let next = head_inner.next.take().unwrap();
                let mut next = next.inner.write().await;
                next.prev = None;
            }
        }
        head_inner.prev = None;
        head_inner.bucket = None;

        head_opt
    }
}

const ST_INIT: u8 = 0;
const ST_CANCELLED: u8 = 1;
const ST_EXPIRED: u8 = 2;

#[derive(Clone)]
pub struct Timeout {
    inner: Arc<RwLock<WheelTimeoutInner>>,
    task: Arc<Mutex<Option<BoxedAsyncFn>>>,
    state: Arc<AtomicU8>,
    timer: WheelTimer,
}

type BoxedAsyncFn = Pin<Box<dyn Future<Output=()> + Send>>;

struct WheelTimeoutInner {
    deadline: u64,
    remaining_rounds: u64,
    next: Option<Timeout>,
    prev: Option<Timeout>,
    bucket: Option<WheelBucket>,
}

impl PartialEq for Timeout {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Timeout {
    fn new(timer: WheelTimer, deadline: u64, task: BoxedAsyncFn) -> Self {
        Timeout {
            inner: Arc::new(RwLock::new(WheelTimeoutInner {
                deadline,
                remaining_rounds: 0,
                next: None,
                prev: None,
                bucket: None,
            })),
            task: Arc::new(Mutex::new(Some(task))),
            state: Arc::new(AtomicU8::new(ST_INIT)),
            timer,
        }
    }

    async fn expire(&self) {
        match self.state.compare_exchange(ST_INIT, ST_EXPIRED, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst) {
            Ok(_) => {
                let task = {
                    let mut inner = self.task.lock().await;
                    inner.take()
                };
                if let Some(task) = task {
                    tokio::spawn(task);
                }
            }
            Err(_) => {}
        }
    }

    // worker线程调用
    async fn remove(&self) {
        let timeout_inner = self.inner.read().await;
        if timeout_inner.bucket.is_some() {
            let bucket = timeout_inner.bucket.clone().unwrap();
            drop(timeout_inner);
            let mut bucket = bucket.inner.write().await;
            let mut timeout_inner = self.inner.write().await;
            WheelBucket::remove(&mut bucket, self, &mut timeout_inner).await;
        } else {
            self.timer.inner.pending_timeouts.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
        }
    }
    fn timer(&self) -> WheelTimer {
        self.timer.clone()
    }

    pub fn is_expired(&self) -> bool {
        self.state.load(std::sync::atomic::Ordering::SeqCst) == ST_EXPIRED
    }

    pub fn is_cancelled(&self) -> bool {
        self.state.load(std::sync::atomic::Ordering::SeqCst) == ST_CANCELLED
    }

    pub async fn cancel(&self) -> bool {
        match self.state.compare_exchange(ST_INIT, ST_CANCELLED, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst) {
            Ok(_) => {
                let timer = self.timer();
                if let Ok(mut cancelled) = timer.inner.cancelled_queue.try_lock() {
                    cancelled.push_back(self.clone());
                }
                true
            }
            Err(_) => {
                false
            }
        }
    }
}

#[derive(Clone)]
pub struct CountDownLatch {
    notify: Arc<Notify>,
    count: Arc<AtomicUsize>,
}

impl CountDownLatch {
    pub fn new(count: usize) -> Self {
        CountDownLatch {
            notify: Arc::new(Notify::new()),
            count: Arc::new(AtomicUsize::new(count)),
        }
    }

    pub fn count_down(&self) {
        let prev = self.count.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
        if prev == 1 {
            self.notify.notify_waiters();
        }
    }

    pub async fn wait(&self) {
        if self.count.load(std::sync::atomic::Ordering::SeqCst) == 0 {
            return; // already done
        }

        let notified = self.notify.notified();
        if self.count.load(std::sync::atomic::Ordering::SeqCst) == 0 {
            return; // check again to avoid race
        }
        notified.await;
    }

    pub fn remaining(&self) -> usize {
        self.count.load(std::sync::atomic::Ordering::SeqCst)
    }
}