Переглянути джерело

More code to delay file system events.

The code to process the events is still missing.
Mathias Gottschlag 5 роки тому
джерело
коміт
3eadbe2b8f
1 змінених файлів з 73 додано та 7 видалено
  1. 73
    7
      src/file_event_delay.rs

+ 73
- 7
src/file_event_delay.rs Переглянути файл

@@ -1,9 +1,12 @@
1
+use std::collections::VecDeque;
1 2
 use std::io;
3
+use std::mem;
2 4
 use std::pin::Pin;
3 5
 use std::task::{Context, Poll};
4 6
 use std::time::Duration;
5 7
 
6 8
 use futures::stream::Stream;
9
+use tokio::time::{self, Interval};
7 10
 
8 11
 /// A stream which delays inotify events and combines them whenever possible.
9 12
 ///
@@ -15,8 +18,18 @@ pub struct FileEventDelay<T>
15 18
 where
16 19
     T: Stream<Item = io::Result<inotify::EventOwned>>,
17 20
 {
18
-    input: T,
21
+    input: Pin<Box<T>>,
22
+
19 23
     min_delay: Duration,
24
+    timer: Option<Pin<Box<Interval>>>,
25
+
26
+    /// Inotify events which are queued for processing.
27
+    ///
28
+    /// Whenever the timer expires, the events from the second array are processed and the content
29
+    /// of the first array is moved to the second. This way, each event is in the queue for a
30
+    /// duration of at least `min_delay`.
31
+    event_queue: (Vec<inotify::EventOwned>, Vec<inotify::EventOwned>),
32
+    processed_events: VecDeque<FileEvent>,
20 33
 }
21 34
 
22 35
 impl<T> FileEventDelay<T>
@@ -24,7 +37,21 @@ where
24 37
     T: Stream<Item = io::Result<inotify::EventOwned>>,
25 38
 {
26 39
     pub fn new(input: T, min_delay: Duration) -> Self {
27
-        Self { input, min_delay }
40
+        Self {
41
+            input: Box::pin(input),
42
+            min_delay,
43
+            timer: None,
44
+            event_queue: (Vec::new(), Vec::new()),
45
+            processed_events: VecDeque::new(),
46
+        }
47
+    }
48
+
49
+    fn process_events(&mut self) {
50
+        // TODO
51
+
52
+        // Swap the arrays. This code assumes that the second array is now empty.
53
+        // TODO: Check the assumption.
54
+        mem::swap(&mut self.event_queue.0, &mut self.event_queue.1);
28 55
     }
29 56
 }
30 57
 
@@ -34,11 +61,50 @@ where
34 61
 {
35 62
     type Item = io::Result<FileEvent>;
36 63
 
37
-    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
38
-        // TODO: Two arrays, fill one array with inotify events. Whenever a periodic timer elapses,
39
-        // parse contents of the other array and clear it, and swap the two arrays.
40
-        // TODO
41
-        panic!("Not yet implemented.");
64
+    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
65
+        // Safe, as we will not move self_.
66
+        let self_ = unsafe { self.get_unchecked_mut() };
67
+
68
+        if self_.processed_events.is_empty() {
69
+            // Fill the first array with incoming inotify events.
70
+            while let Poll::Ready(inotify_event) = Pin::as_mut(&mut self_.input).poll_next(cx) {
71
+                if inotify_event.is_some() {
72
+                    self_.event_queue.0.push(inotify_event.unwrap().unwrap());
73
+                } else {
74
+                    // TODO: Stream exhausted. Can this ever happen?
75
+                    return Poll::Ready(None);
76
+                }
77
+            }
78
+
79
+            // If the first array is not empty, start a timer for processing if no timer is already
80
+            // running.
81
+            if !self_.event_queue.0.is_empty() && self_.timer.is_none() {
82
+                self_.timer = Some(Box::pin(time::interval(self_.min_delay)));
83
+            }
84
+
85
+            // If the timer elapsed, process the events from the second array and move the content
86
+            // of the first array to the second.
87
+            if let Poll::Ready(Some(_)) =
88
+                Pin::as_mut(&mut self_.timer.as_mut().unwrap()).poll_next(cx)
89
+            {
90
+                self_.process_events();
91
+
92
+                // If no unprocessed events are available, stop the timer to reduce CPU
93
+                // consumption.
94
+                if self_.event_queue.1.is_empty() {
95
+                    self_.timer = None;
96
+                }
97
+                // Return the first event if possible.
98
+                if let Some(next_event) = self_.processed_events.pop_front() {
99
+                    return Poll::Ready(Some(Ok(next_event)));
100
+                }
101
+            }
102
+
103
+            Poll::Pending
104
+        } else {
105
+            let next_event = self_.processed_events.pop_front().unwrap();
106
+            Poll::Ready(Some(Ok(next_event)))
107
+        }
42 108
     }
43 109
 }
44 110
 

Завантаження…
Відмінити
Зберегти