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
use failure::Error;
use futures::{Future, Stream};
use std::time::Duration;
use tokio_executor::spawn;
use tokio_timer::Interval;
pub trait GarbageCollector {
fn collect(&self) -> Box<dyn Future<Item = (), Error = Error> + Send>;
}
pub fn spawn_gc<C>(duration: Duration, collector: C)
where
C: GarbageCollector + Send + 'static,
{
spawn(
Interval::new_interval(duration)
.for_each(move |_| {
collector.collect().then(|r| {
if let Err(e) = r {
log::error!("Failed to clear old sessions: {:?}", e);
}
Ok(())
})
})
.map_err(|e| log::error!("Failed to spawn session GC: {:?}", e)),
);
}