Comments (20)
This sounds like a sensible solution. I’ll add it.
from rustbreak.
tempfile
crate implements it nicely:
use tempfile::NamedTempFile;
…
let tmp_path = NamedTempFile::new_in(self.path.parent().unwrap())?;
fs::write(&tmp_path, serialize()?)?;
tmp_path.persist(&self.path)?;
Ensures the temp file is deleted on error, etc.
from rustbreak.
I think it would be extremely counter-intuitive to have the save behaviour depend on the creation method (file or path). I think it would be a lot clearer to split it out to two back-ends (say FileBackend
without atomic saves and PathBackend
with atomic saves).
Is backwards compatibility really required? From version 1 to 2 you can make breaking changes (and 2 hasn't been released yet).
from rustbreak.
I'm looking forward to this! I'm using panic="abort"
and saving app state periodically to disk using rustbreak. If a panic happens in the middle of the saving the db, it corrupts the db file..
@kornelski With the current version of rustbreak (and v1), it wouldn't corrupt the db file with panic="unwind"
if a panic happens in the middle of the saving the db, right?
from rustbreak.
The process can get SIGTERM/SIGKILL at any time. I'm most worried about unfortunate press of Ctrl-C corrupting the db.
from rustbreak.
Ah yes, that happens more often than panics :)
from rustbreak.
Any update on this?
In my experience, corrupting the db happens a lot when pressing Ctrl-C.
My application saves its state to the db every few seconds, and writing takes some time even though I use the binary encoding. So the chance of Ctrl-C interrupting the write are very high.
I'm really looking forward to atomic file saves.
from rustbreak.
I've used this instead: https://gitlab.com/crates.rs/simple_cache/blob/master/src/kv.rs
from rustbreak.
In my experience, corrupting the db happens a lot when pressing Ctrl-C.
My application saves its state to the db every few seconds, and writing takes some time even though I use the binary encoding. So the chance of Ctrl-C interrupting the write are very high.
You can use some signal handling to migrate this risk (except SIGKILL of course, which isn't really a signal...). The cli-wg has a short introduction to signal handling in rust: https://rust-lang-nursery.github.io/cli-wg/in-depth/signals.html. Atomic saves would be very nice though.
from rustbreak.
@niluxv But using the ctrlc
crate to intercept Ctrl-C doesn't work when running my exe through cargo watch -x run
which I'm always using during development!
rust-lang/cargo#4575
from rustbreak.
Ah, oke. cargo-watch
should probably forward signals. I saw you already created an issue for that.
from rustbreak.
@TheNeikos Any update on this? Btw: https://crates.io/crates/atomicwrites
from rustbreak.
I thought lets give it a try, but there is a catch. You need to close the file before you can replace it by the temporary file. I can think of 3 options:
- Change the
Backend
(trait) API to give ownership ofself
toput_data
(and return a new instance in theResult
). - Do not keep an open file handle in
FileBackend
, but rather a path to the file (This is the trick used byatomicwrites
). - Use a crate like
take_mut
(or the more recentreplace_with
). This will pull in a dependancy withunsafe
code though.
from rustbreak.
If you need fast reads (that is, Backend::get_data
) I'd advice against option 2, since it requires to open the file on every read. But most of the time you are probably reading the in memory representation (not loading from the backend).
from rustbreak.
Yes, in the current state, adding this is somewhat difficult generally. Since the FileBackend
only takes a File, it has no notion of a path and thus can't re-open it. I'd accept a PR adding a backwards compatible option if the FileBackend
was created with a path where it would re-open it.
from rustbreak.
@niluxv That's actually a good idea. And you're correct, it's only in RC, I forgot that it is technically a pre-release and thus could introduce breaking changes from 1 to 2.
Adding a PathBackend
is also feasible and the correct path forward I feel. It side-steps the constructor specific problem.
from rustbreak.
I will create a PR.
from rustbreak.
I think this can be closed now. Atomic saves are now available through the PathBackend
backend (and a database with this backend is aliased PathDatabase
).
from rustbreak.
Any idea when this version with atomic saving will be available on crates.io? :)
Or can you at least please re-generate the docs based on master?
from rustbreak.
@TheNeikos Maybe release a 2.0.0-rc4
?
Before the final stable 2.0.0
I'd like to solve issue #67 (if you agree to the proposed solution). That would be a breaking change, so it has to be done before 2.0.0
.
Updating the dependencies as in pull request #73 is also a breaking change. Maybe this could be merged even before an rc4 release, since it updates a vulnarable dependency?
from rustbreak.
Related Issues (20)
- Missing detailed information from the back-end HOT 2
- Errors with newest stable Rust HOT 5
- Problems with Ron and json Value integers HOT 6
- Load performance problems HOT 15
- [FR] we should be able to minify the output HOT 6
- Bincode not loading in v2 rc3 HOT 14
- Database::try_read() HOT 2
- FileBackend::from_file / Database::from_file docs missing: Does it load()? HOT 2
- Loading database with empty File fails HOT 18
- Update RON HOT 1
- Transition to Rust edition 2018
- Use `data.read()` instead of `data.write()` in `save()` HOT 1
- Describing generic database HOT 6
- Run `cargo fmt` HOT 1
- More checks in CI HOT 3
- UB with yaml DeSer HOT 5
- RUSTSEC-2020-0077: memmap is unmaintained
- Out of memory if file size > memory size HOT 1
- support speedy https://docs.rs/speedy/ HOT 1
- wasm support + wasi support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rustbreak.