|
@@ -8,6 +8,7 @@ use std::time::{Duration, SystemTime};
|
|
|
|
|
|
use dashmap::DashMap;
|
|
|
use log::{error, info};
|
|
|
+use rand::Rng;
|
|
|
use serde::Serialize;
|
|
|
use tokio::time::{self, Instant};
|
|
|
use warp::{filters::BoxedFilter, ws::Ws, Filter, Rejection, Reply};
|
|
@@ -208,19 +209,23 @@ async fn cleaner(state: ServerState, expiry_days: u32) {
|
|
|
}
|
|
|
|
|
|
const PERSIST_INTERVAL: Duration = Duration::from_secs(3);
|
|
|
+const PERSIST_INTERVAL_JITTER: Duration = Duration::from_secs(1);
|
|
|
|
|
|
/// Persists changed documents after a fixed time interval.
|
|
|
async fn persister(id: String, rustpad: Arc<Rustpad>, db: Database) {
|
|
|
let mut last_revision = 0;
|
|
|
while !rustpad.killed() {
|
|
|
- time::sleep(PERSIST_INTERVAL).await;
|
|
|
+ let interval = PERSIST_INTERVAL
|
|
|
+ + rand::thread_rng().gen_range(Duration::ZERO..=PERSIST_INTERVAL_JITTER);
|
|
|
+ time::sleep(interval).await;
|
|
|
let revision = rustpad.revision();
|
|
|
if revision > last_revision {
|
|
|
info!("persisting revision {} for id = {}", revision, id);
|
|
|
if let Err(e) = db.store(&id, &rustpad.snapshot()).await {
|
|
|
error!("when persisting document {}: {}", id, e);
|
|
|
+ } else {
|
|
|
+ last_revision = revision;
|
|
|
}
|
|
|
- last_revision = revision;
|
|
|
}
|
|
|
}
|
|
|
}
|