Skip to content

Commit 235c69c

Browse files
authored
fix(iroh-docs): ensure docs db write txn gets closed regularly under all circumstances (#2474)
## Description The docs db uses batching to reduce the number of write transactions. Before this, under certain circumstances (lots of activity, exclusively writes) the transaction would remain open for a long time. This adds logic to modify to make sure that transactions will be committed when they get too old in any case. ## Breaking Changes None ## Notes & open questions ## Change checklist - [x] Self-review. - [x] ~~Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant.~~ - [x] ~~Tests if relevant.~~ - [x] ~~All breaking changes documented.~~
1 parent 3002deb commit 235c69c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

iroh-docs/src/actor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
};
2929

3030
const ACTION_CAP: usize = 1024;
31-
const MAX_COMMIT_DELAY: Duration = Duration::from_millis(500);
31+
pub(crate) const MAX_COMMIT_DELAY: Duration = Duration::from_millis(500);
3232

3333
#[derive(derive_more::Debug, derive_more::Display)]
3434
enum Action {

iroh-docs/src/store/fs.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
num::NonZeroU64,
88
ops::Bound,
99
path::Path,
10-
time::Duration,
1110
};
1211

1312
use anyhow::{anyhow, Result};
@@ -17,6 +16,7 @@ use rand_core::CryptoRngCore;
1716
use redb::{Database, DatabaseError, ReadableMultimapTable, ReadableTable, ReadableTableMetadata};
1817

1918
use crate::{
19+
actor::MAX_COMMIT_DELAY,
2020
keys::Author,
2121
ranger::{Fingerprint, Range, RangeEntry},
2222
sync::{Entry, EntrySignature, Record, RecordIdentifier, Replica, SignedEntry},
@@ -188,7 +188,7 @@ impl Store {
188188
TransactionAndTables::new(tx)?
189189
}
190190
CurrentTransaction::Write(w) => {
191-
if w.since.elapsed() > Duration::from_millis(500) {
191+
if w.since.elapsed() > MAX_COMMIT_DELAY {
192192
tracing::debug!("committing transaction because it's too old");
193193
w.commit()?;
194194
let tx = self.db.begin_write()?;
@@ -224,7 +224,16 @@ impl Store {
224224
let tx = self.db.begin_write()?;
225225
TransactionAndTables::new(tx)?
226226
}
227-
CurrentTransaction::Write(w) => w,
227+
CurrentTransaction::Write(w) => {
228+
if w.since.elapsed() > MAX_COMMIT_DELAY {
229+
tracing::debug!("committing transaction because it's too old");
230+
w.commit()?;
231+
let tx = self.db.begin_write()?;
232+
TransactionAndTables::new(tx)?
233+
} else {
234+
w
235+
}
236+
}
228237
CurrentTransaction::Read(_) => {
229238
let tx = self.db.begin_write()?;
230239
TransactionAndTables::new(tx)?

0 commit comments

Comments
 (0)