chiark / gitweb /
wip log expiry
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 17 Oct 2020 13:05:24 +0000 (14:05 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 17 Oct 2020 13:05:24 +0000 (14:05 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock.example
Cargo.toml
src/gamestate.rs
src/global.rs
src/imports.rs
src/updates.rs

index 51e03b18c353dc60830d8e247d891e8d0f4f9c72..d69c21f3267e6033559bdf49e0b0b8f5633cd0be 100644 (file)
@@ -186,6 +186,12 @@ dependencies = [
  "byte-tools",
 ]
 
+[[package]]
+name = "boolinator"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9"
+
 [[package]]
 name = "bumpalo"
 version = "3.4.0"
@@ -1065,6 +1071,7 @@ dependencies = [
  "anyhow",
  "argparse",
  "arrayvec",
+ "boolinator",
  "chrono",
  "chrono-tz",
  "delegate",
index ee2385ad389a738c64545c1e6aba68a6924ce603..d7901ca2761cb1b96110699182e314b5937b747a 100644 (file)
@@ -20,6 +20,7 @@ otter-zcoord = { path = "zcoord" }
 anyhow = "1"
 argparse = "0.2"
 arrayvec = "0"
+boolinator = "2"
 chrono = "0.4"
 chrono-tz = "0.5"
 delegate = "0.4"
index 233496de2150183109d765ff7358e2782cb10c94..3d69d2cb9145e4f304261551bfb4f81c9b053faa 100644 (file)
@@ -45,7 +45,7 @@ pub struct GameState {
   pub pieces : Pieces,
   pub players : PlayerMap,
   pub gen : Generation,
-  pub log : Vec<(Generation, Arc<CommittedLogEntry>)>, // xxx expiry
+  pub log : VecDeque<(Generation, Arc<CommittedLogEntry>)>, // xxx expiry
   pub max_z : ZCoord,
 }
 
@@ -217,7 +217,6 @@ impl Debug for Html {
 
 // ---------- game state - rendering etc. ----------
 
-
 impl PieceState {
   #[throws(IE)]
   pub fn prep_piecestate(&self, p: &dyn Piece, pri : &PieceRenderInstructions)
@@ -283,6 +282,31 @@ impl<T> PieceExt for T where T: Piece + ?Sized {
   }
 }
 
+// ---------- log expiry ==========
+
+impl GameState {
+  pub fn expire_old_logs(&mut self, cutoff: Timestamp) {
+    fn want_trim(gs: &GameState, cutoff: Timestamp) -> bool {
+      (||{
+        let e = gs.log.get(1)?;
+        (e.1.when < cutoff).as_option()
+      })().is_some()
+    };
+
+    if want_trim(self, cutoff) {
+      while want_trim(self, cutoff) {
+        self.log.pop_front();
+      }
+      let front = self.log.front_mut().unwrap();
+      let front = &mut front.1;
+      let logent = LogEntry {
+        html: Html::lit("Earlier log entries expired."),
+      };
+      *front = Arc::new(CommittedLogEntry { logent, when: front.when });
+    }
+  }
+}
+
 // ========== ad-hoc and temporary ==========
 
 pub fn make_pieceid_visible(p : PieceId) -> VisiblePieceId {
index ba335682b4ecfbc1b516c5fee943e55225b6ed86..fd718f8b2cc77b469fd819aaf7eb560a7b294d10 100644 (file)
@@ -17,6 +17,8 @@ const MAX_CLIENT_INACTIVITY : Duration = Duration::from_secs(200);
 
 const GAME_SAVE_LAG : Duration = Duration::from_millis(500);
 
+const MAX_LOG_AGE : Duration = Duration::from_secs(10 * 86400);
+
 #[derive(Hash,Ord,PartialOrd,Eq,PartialEq,Serialize)]
 #[repr(transparent)]
 pub struct RawTokenVal(str);
index dcd5754f745a7bbf7e70908ff3b391f91888615c..5e6b3cfb603dcbff83fcec475943046bcbc482e6 100644 (file)
@@ -35,6 +35,8 @@ pub use std::env;
 pub use std::process::exit;
 pub use std::borrow::Cow;
 
+pub use boolinator::Boolinator as _;
+
 pub use thiserror::Error;
 pub use anyhow::{Context,anyhow};
 pub use fehler::{throws,throw};
index 59b090b3ad16e84a25543cba2eb0a6f0e8c0e6d1..8c4418a4186e6ff7f056079f7e4bb0ea117a2f5c 100644 (file)
@@ -412,11 +412,11 @@ impl<'r> PrepareUpdatesBuffer<'r> {
     let now = Timestamp::now();
     for logent in logents {
       let when = iter::once(now).chain(
-        self.g.gs.log.last().map(|l| l.1.when)
+        self.g.gs.log.back().map(|l| l.1.when)
       ).max().unwrap();
       let logent = Arc::new(CommittedLogEntry { when, logent });
       let gen = self.gen();
-      self.g.gs.log.push((gen, logent.clone()));
+      self.g.gs.log.push_back((gen, logent.clone()));
       self.us.push(PreparedUpdateEntry::Log(logent));
     }
   }