Improve persistence logic
This commit is contained in:
parent
124ff15d3d
commit
d1ed9883db
3 changed files with 11 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1067,6 +1067,7 @@ dependencies = [
|
|||
"operational-transform",
|
||||
"parking_lot",
|
||||
"pretty_env_logger",
|
||||
"rand 0.8.3",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
|
|
|
@ -20,6 +20,7 @@ sqlx = { version = "0.5.9", features = ["runtime-tokio-rustls", "sqlite"] }
|
|||
tokio = { version = "1.6.1", features = ["full", "test-util"] }
|
||||
tokio-stream = "0.1.6"
|
||||
warp = "0.3.1"
|
||||
rand = "0.8"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.2.0"
|
||||
|
|
|
@ -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,25 @@ 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
|
||||
+ Duration::from_millis(
|
||||
rand::thread_rng().gen_range(0..=PERSIST_INTERVAL_JITTER.as_millis() as u64),
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue