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
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.
pub use self::implementation::Timer;
#[cfg(not(feature = "use-mock-crust"))]
mod implementation {
use action::Action;
use itertools::Itertools;
use maidsafe_utilities::thread::{self, Joiner};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::sync::mpsc::{self, Receiver, RecvError, RecvTimeoutError, SyncSender};
use std::time::{Duration, Instant};
use types::RoutingActionSender;
struct Detail {
expiry: Instant,
token: u64,
}
/// Simple timer.
#[derive(Clone)]
pub struct Timer {
inner: Rc<RefCell<Inner>>,
}
struct Inner {
next_token: u64,
tx: SyncSender<Detail>,
_worker: Joiner,
}
impl Timer {
/// Creates a new timer, passing a channel sender used to send `Timeout` events.
pub fn new(sender: RoutingActionSender) -> Self {
let (tx, rx) = mpsc::sync_channel(1);
let worker = thread::named("Timer", move || Self::run(sender, rx));
Timer {
inner: Rc::new(RefCell::new(Inner {
next_token: 0,
tx,
_worker: worker,
})),
}
}
// TODO Do proper error handling here by returning a result - currently complying it with
// existing code and logging and error
/// Schedules a timeout event after `duration`. Returns a token that can be used to identify
/// the timeout event.
pub fn schedule(&self, duration: Duration) -> u64 {
let mut inner = self.inner.borrow_mut();
let token = inner.next_token;
inner.next_token = token.wrapping_add(1);
let detail = Detail {
expiry: Instant::now() + duration,
token,
};
inner.tx.send(detail).map(|()| token).unwrap_or_else(|e| {
error!("Timer could not be scheduled: {:?}", e);
0
})
}
fn run(sender: RoutingActionSender, rx: Receiver<Detail>) {
let mut deadlines: BTreeMap<Instant, Vec<u64>> = Default::default();
loop {
let r = if let Some(t) = deadlines.keys().next() {
let now = Instant::now();
if *t > now {
let duration = *t - now;
match rx.recv_timeout(duration) {
Ok(d) => Some(d),
Err(RecvTimeoutError::Timeout) => None,
Err(RecvTimeoutError::Disconnected) => break,
}
} else {
None
}
} else {
match rx.recv() {
Ok(d) => Some(d),
Err(RecvError) => break,
}
};
if let Some(Detail { expiry, token }) = r {
deadlines.entry(expiry).or_insert_with(Vec::new).push(token);
}
let now = Instant::now();
let expired_list = deadlines
.keys()
.take_while(|&&deadline| deadline < now)
.cloned()
.collect_vec();
for expired in expired_list {
// Safe to call `expect()` as we just got the key we're removing from
// `deadlines`.
let tokens = deadlines.remove(&expired).expect("Bug in `BTreeMap`.");
for token in tokens {
let _ = sender.send(Action::Timeout(token));
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use action::Action;
use maidsafe_utilities::event_sender::MaidSafeEventCategory;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, Instant};
use types::RoutingActionSender;
#[test]
fn schedule() {
let (action_sender, action_receiver) = mpsc::channel();
let (category_sender, category_receiver) = mpsc::channel();
let routing_event_category = MaidSafeEventCategory::Routing;
let sender = RoutingActionSender::new(
action_sender,
routing_event_category,
category_sender.clone(),
);
let interval = Duration::from_millis(500);
let instant_when_added;
let check_no_events_received = || {
let category = category_receiver.try_recv();
assert!(
category.is_err(),
"Expected no event, but received {:?}",
category
);
let action = action_receiver.try_recv();
assert!(
action.is_err(),
"Expected no event, but received {:?}",
action
);
};
{
let timer = Timer::new(sender);
// Add deadlines, the first to time out after 2.5s, the second after 2.0s, and so on
// down to 500ms.
let count = 5;
for i in 0..count {
let timeout = interval * (count - i);
let token = timer.schedule(timeout);
assert_eq!(token, u64::from(i));
}
// Ensure timeout notifications are received correctly.
thread::sleep(Duration::from_millis(100));
for i in 0..count {
check_no_events_received();
thread::sleep(interval);
let category = category_receiver.try_recv();
match category.expect("Should have received a category.") {
MaidSafeEventCategory::Routing => (),
unexpected_category => {
panic!(
"Expected `MaidSafeEventCategory::Routing`, but received {:?}",
unexpected_category
);
}
}
let action = action_receiver.try_recv();
match action.expect("Should have received an action.") {
Action::Timeout(token) => assert_eq!(token, u64::from(count - i - 1)),
unexpected_action => {
panic!(
"Expected `Action::Timeout`, but received {:?}",
unexpected_action
);
}
}
}
// Add deadline and check that dropping `timer` doesn't fire a timeout notification,
// and that dropping doesn't block until the deadline has expired.
instant_when_added = Instant::now();
let _ = timer.schedule(interval);
}
assert!(
Instant::now() - instant_when_added < interval,
"`Timer::drop()` is blocking."
);
thread::sleep(interval + Duration::from_millis(100));
check_no_events_received();
}
#[test]
fn heavy_duty_time_out() {
let (action_sender, _action_receiver) = mpsc::channel();
let (category_sender, _category_receiver) = mpsc::channel();
let routing_event_category = MaidSafeEventCategory::Routing;
let sender = RoutingActionSender::new(
action_sender,
routing_event_category,
category_sender.clone(),
);
let timer = Timer::new(sender);
for _ in 0..1000 {
let _ = timer.schedule(Duration::new(0, 3000));
}
}
}
}
#[cfg(feature = "use-mock-crust")]
mod implementation {
use fake_clock::FakeClock as Instant;
use itertools::Itertools;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::time::Duration;
use types::RoutingActionSender;
struct Inner {
next_token: u64,
deadlines: BTreeMap<Instant, Vec<u64>>,
}
#[derive(Clone)]
pub struct Timer {
inner: Rc<RefCell<Inner>>,
}
impl Timer {
pub fn new(_action_sender: RoutingActionSender) -> Self {
Timer {
inner: Rc::new(RefCell::new(Inner {
next_token: 0,
deadlines: Default::default(),
})),
}
}
pub fn schedule(&self, duration: Duration) -> u64 {
let mut inner = self.inner.borrow_mut();
let token = inner.next_token;
inner.next_token = token.wrapping_add(1);
inner
.deadlines
.entry(Instant::now() + duration)
.or_insert_with(Vec::new)
.push(token);
token
}
pub fn get_timed_out_tokens(&mut self) -> Vec<u64> {
let mut inner = self.inner.borrow_mut();
let now = Instant::now();
let expired_list = inner
.deadlines
.keys()
.take_while(|&&deadline| deadline < now)
.cloned()
.collect_vec();
let mut expired_tokens = Vec::new();
for expired in expired_list {
// Safe to call `unwrap!()` as we just got the key we're removing from
// `deadlines`.
let tokens = unwrap!(inner.deadlines.remove(&expired));
expired_tokens.extend(tokens);
}
expired_tokens
}
}
}