about summary refs log tree commit diff
path: root/src/database/mod.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-05-01 04:35:16 +0300
committerVika <vika@fireburn.ru>2022-05-01 04:35:16 +0300
commit122361795b3b1376c6ba03ed6b160e9b89da93d7 (patch)
tree71fe8dc080a74952c29da04d57689c2643e7f211 /src/database/mod.rs
parente2bc26e907c10def259f52401804f7f6d00c498c (diff)
FileStorage: lockless reads and atomic writes
  - Reads don't lock anymore. At all.
  - Writes create a temporary file and use `rename(2)` to atomically
    replace it
  - since OpenOptions::create_new(true) is used, tempfile creation is
    atomic (and since tempfile names are per-post, a post can only be
    edited by one request at a time)
  - Since written files get atomically replaced, readers can't read a
    corrupted file

Potential pitfalls:
1. This approach is not covered by unit tests (yet)
2. Stale tempfiles can prevent editing posts (can be solved by
throwing out tempfiles that are older than, say, a day)
3. Crashed edits can leave stale tempfiles (honestly that sounds
better than corrupting the whole database, doesn't sound like a bug to
me at all!)
Diffstat (limited to 'src/database/mod.rs')
-rw-r--r--src/database/mod.rs5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs
index 57223f8..0d98dd4 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -35,12 +35,14 @@ pub enum ErrorKind {
     /// The user's query or request to the database was malformed. Used whenever the database processes
     /// the user's query directly, such as when editing posts inside of the database (e.g. Redis backend)
     BadRequest,
+    /// the user's query collided with an in-flight request and needs to be retried
+    Conflict,
     ///  - ErrorKind::Other - when something so weird happens that it becomes undescribable.
     Other,
 }
 
 /// Enum representing settings that might be stored in the site's database.
-#[derive(Serialize, Debug, Clone, Copy)]
+#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
 #[serde(rename_all = "snake_case")]
 pub enum Settings {
     /// The name of the website -- displayed in the header and the browser titlebar.
@@ -87,6 +89,7 @@ impl std::fmt::Display for StorageError {
             ErrorKind::PermissionDenied => write!(f, "permission denied: "),
             ErrorKind::NotFound => write!(f, "not found: "),
             ErrorKind::BadRequest => write!(f, "bad request: "),
+            ErrorKind::Conflict => write!(f, "conflict with an in-flight request or existing data: "),
             ErrorKind::Other => write!(f, "generic storage layer error: "),
         } {
             Ok(_) => write!(f, "{}", self.msg),