feat(exex): write notification files atomically (#11264)

This commit is contained in:
joshieDo
2024-09-26 23:23:09 +02:00
committed by GitHub
parent 13a3c2c8cc
commit da6b1e7c64
3 changed files with 83 additions and 52 deletions

View File

@ -1,6 +1,5 @@
use std::{
fs::File,
io::{Read, Write},
ops::RangeInclusive,
path::{Path, PathBuf},
};
@ -95,7 +94,8 @@ impl Storage {
debug!(?file_path, "Reading notification from WAL");
let mut file = File::open(&file_path)?;
read_notification(&mut file)
// TODO(alexey): use rmp-serde when Alloy and Reth serde issues are resolved
Ok(serde_json::from_reader(&mut file)?)
}
/// Writes the notification to the file with the given id.
@ -108,27 +108,13 @@ impl Storage {
let file_path = self.file_path(file_id);
debug!(?file_path, "Writing notification to WAL");
let mut file = File::create_new(&file_path)?;
write_notification(&mut file, notification)?;
Ok(())
Ok(reth_fs_util::atomic_write_file(&file_path, |file| {
// TODO(alexey): use rmp-serde when Alloy and Reth serde issues are resolved
serde_json::to_writer(file, notification)
})?)
}
}
// TODO(alexey): use rmp-serde when Alloy and Reth serde issues are resolved
fn write_notification(mut w: &mut impl Write, notification: &ExExNotification) -> eyre::Result<()> {
// rmp_serde::encode::write(w, notification)?;
serde_json::to_writer(&mut w, notification)?;
w.flush()?;
Ok(())
}
fn read_notification(r: &mut impl Read) -> eyre::Result<ExExNotification> {
// Ok(rmp_serde::from_read(r)?)
Ok(serde_json::from_reader(r)?)
}
#[cfg(test)]
mod tests {
use std::sync::Arc;