perf: debounce store writes and add retry loop on Redb transaction collisions
This commit is contained in:
1 parent
533adfd41b
commit
3efee96230
3 files changed
+22
-9
No files matched your search
+16
-5
@@ -21,7 +21,9 @@ impl<T: DeserializeOwned + Default + Serialize + Clone + Send + Sync + 'static>
|
||||
|
||||
tokio::spawn(async move {
|
||||
while rx.recv().await.is_some() {
|
||||
// Drain any other pending notifications so we batch writes
|
||||
// Debounce window: wait 150ms to batch rapid sequential mutations
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(150)).await;
|
||||
// Drain any pending notifications accumulated during the debounce window
|
||||
while rx.try_recv().is_ok() {}
|
||||
|
||||
let db_inner = db_clone.clone();
|
||||
@@ -35,11 +37,20 @@ impl<T: DeserializeOwned + Default + Serialize + Clone + Send + Sync + 'static>
|
||||
|
||||
if let Some(json_data) = json_data {
|
||||
let _ = tokio::task::spawn_blocking(move || {
|
||||
if let Ok(write_txn) = db_inner.begin_write() {
|
||||
if let Ok(mut table) = write_txn.open_table(STORE_TABLE) {
|
||||
let _ = table.insert(key_inner.as_str(), json_data.as_slice());
|
||||
// Retry up to 10 times with 30ms backoff if another Store holds write transaction
|
||||
for _ in 0..10 {
|
||||
match db_inner.begin_write() {
|
||||
Ok(write_txn) => {
|
||||
if let Ok(mut table) = write_txn.open_table(STORE_TABLE) {
|
||||
let _ = table.insert(key_inner.as_str(), json_data.as_slice());
|
||||
}
|
||||
let _ = write_txn.commit();
|
||||
break;
|
||||
}
|
||||
Err(_) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(30));
|
||||
}
|
||||
}
|
||||
let _ = write_txn.commit();
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
Reference in new issue
Block a user