From aafdeb9ffaf9a993ca4462b3422667e04469b2e3 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Fri, 20 Sep 2024 16:09:35 -0400 Subject: Expire messages after 90 days. This is intended to manage storage growth. A community with broadly steady traffic will now reach a steady state (ish) where the amount of storage in use stays within a steady band. The 90 day threshold is a spitball; this should be made configurable for the community's needs. I've also hoisted expiry out into the `app` classes, to reduce the amount of non-database work repo types are doing. This should make it easier to make expiry configurable later on. Includes incidental cleanup and style changes. --- src/repo/token.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/repo/token.rs') diff --git a/src/repo/token.rs b/src/repo/token.rs index 5674c92..a2393e3 100644 --- a/src/repo/token.rs +++ b/src/repo/token.rs @@ -1,4 +1,3 @@ -use chrono::TimeDelta; use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction}; use uuid::Uuid; @@ -61,19 +60,16 @@ impl<'c> Tokens<'c> { Ok(()) } - /// Expire and delete all tokens that haven't been used within the expiry - /// interval (right now, 7 days) prior to `expire_at`. Tokens that are in - /// use within that period will be retained. - pub async fn expire(&mut self, expire_at: DateTime) -> Result<(), sqlx::Error> { - // Somewhat arbitrarily, expire after 7 days. - let expired_issue_at = expire_at - TimeDelta::days(7); + /// Expire and delete all tokens that haven't been used more recently than + /// ``expire_at``. + pub async fn expire(&mut self, expire_at: &DateTime) -> Result<(), sqlx::Error> { sqlx::query!( r#" delete from token where last_used_at < $1 "#, - expired_issue_at, + expire_at, ) .execute(&mut *self.0) .await?; @@ -87,7 +83,7 @@ impl<'c> Tokens<'c> { pub async fn validate( &mut self, secret: &str, - used_at: DateTime, + used_at: &DateTime, ) -> Result { // I would use `update … returning` to do this in one query, but // sqlite3, as of this writing, does not allow an update's `returning` -- cgit v1.2.3