mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(exex): write notification files atomically (#11264)
This commit is contained in:
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user