Skip to content

Commit aebdde1

Browse files
committed
only persist the index if the last update was more than an hour ago
1 parent 806e521 commit aebdde1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/bin/server/worker.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use rla::index::IndexStorage;
77
use std::collections::{HashSet, VecDeque};
88
use std::hash::Hash;
99
use std::str;
10+
use std::time::{Instant, Duration};
11+
12+
const MINIMUM_DELAY_BETWEEN_INDEX_BACKUPS: Duration = Duration::from_secs(60 * 60);
1013

1114
pub struct Worker {
1215
debug_post: Option<(String, u32)>,
@@ -22,6 +25,8 @@ pub struct Worker {
2225

2326
recently_notified: RecentlySeen<u64>,
2427
recently_learned: RecentlySeen<String>,
28+
29+
last_index_backup: Option<Instant>,
2530
}
2631

2732
impl Worker {
@@ -61,6 +66,8 @@ impl Worker {
6166

6267
recently_notified: RecentlySeen::new(32),
6368
recently_learned: RecentlySeen::new(256),
69+
70+
last_index_backup: None,
6471
})
6572
}
6673

@@ -322,7 +329,16 @@ impl Worker {
322329
}
323330
}
324331

325-
self.index.save(&self.index_file)?;
332+
// To avoid persisting the index too many times to storage, we only persist it after some
333+
// time elapsed since the last save.
334+
match self.last_index_backup {
335+
Some(last) if last.elapsed() >= MINIMUM_DELAY_BETWEEN_INDEX_BACKUPS => {
336+
self.index.save(&self.index_file)?;
337+
self.last_index_backup = Some(Instant::now());
338+
}
339+
Some(_) => {}
340+
None => self.last_index_backup = Some(Instant::now()),
341+
}
326342

327343
Ok(())
328344
}

0 commit comments

Comments
 (0)