Skip to content

Commit

Permalink
aw-datastore: Guarantee DB commit every 15 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Mar 28, 2020
1 parent afbb70c commit 17dff67
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion aw-datastore/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::thread;

use chrono::DateTime;
use chrono::Utc;
use chrono::Duration;

use rusqlite::Connection;
use rusqlite::DropBehavior;
Expand Down Expand Up @@ -150,6 +151,7 @@ impl DatastoreWorker {
};
let mut commit = false;
let mut uncommited_events = 0;
let last_commit_time: DateTime<Utc> = Utc::now();
transaction.set_drop_behavior(DropBehavior::Commit);
loop {
let (request, response_sender) = match self.responder.poll() {
Expand Down Expand Up @@ -262,7 +264,9 @@ impl DatastoreWorker {
}
};
response_sender.respond(response);
if commit || uncommited_events > 100 {
let now: DateTime<Utc> = Utc::now();
let commit_interval_passed: bool = (now - last_commit_time) > Duration::seconds(15);
if commit || commit_interval_passed || uncommited_events > 100 {
break;
};
}
Expand Down

0 comments on commit 17dff67

Please sign in to comment.