forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

sweeper.rs122 lines, 5.5 KiBHistoryRawDownload
1//! The sweeper (architecture section 8): once at boot and then hourly, it removes what has expired.
2//!
3//! - browser sessions past their expiry, with their back-channel logout jobs queued first;
4//! - authorization codes that expired more than a day ago (a day is kept so that a reused code is still
5//! recognised as reuse, which revokes the tokens it issued);
6//! - refresh tokens past their expiry;
7//! - revoked access tokens past their expiry (the revocation has done its work by then);
8//! - directory change rows older than 30 days (a cursor older than the oldest row gets `resync`);
9//! - account links and invitations past their expiry;
10//! - sent mail older than 30 days (its body was blanked when it was sent);
11//! - failed and abandoned mail older than 30 days;
12//! - back-channel deliveries delivered or given up more than 7 days ago;
13//! - audit entries older than a year (`AUDIT_KEEP`).
14//!
15//! Each step runs on its own: one failing step is logged and does not stop the others.
16
17use std::time::Duration;
18
19use im_core::store::Store;
20use im_core::time::{now_stamp, stamp_before, stamp_minus};
21use tokio::sync::watch;
22
23/// The time between two sweeps.
24const EVERY: Duration = Duration::from_secs(60 * 60);
25
26/// How long a directory change row is kept (contracts 3.2 and architecture section 3).
27const DIRECTORY_KEEP: Duration = Duration::from_secs(30 * 24 * 60 * 60);
28
29/// How long an expired authorization code is kept, so a reuse is still seen as reuse.
30const CODE_KEEP: Duration = Duration::from_secs(24 * 60 * 60);
31
32/// How long a sent mail is kept in the outbox before it is deleted (architecture section 8). Failed and
33/// abandoned mail are kept as long.
34const MAIL_KEEP: Duration = Duration::from_secs(30 * 24 * 60 * 60);
35
36/// How long a finished back-channel delivery (sent or given up) is kept. A week is long enough to inspect
37/// a failure on the admin pages.
38const OUTBOX_KEEP: Duration = Duration::from_secs(7 * 24 * 60 * 60);
39
40/// How long an audit entry is kept. A fixed year, not a setting: `docs/architecture.md` section 3 lists it with
41/// the other retention rules. Entries older than this are removed.
42const AUDIT_KEEP: Duration = Duration::from_secs(365 * 24 * 60 * 60);
43
44/// Starts the sweeper. It runs until `stop` is set.
45pub fn spawn(store: Store, mut stop: watch::Receiver<bool>) -> tokio::task::JoinHandle<()> {
46 tokio::spawn(async move {
47 loop {
48 sweep_once(&store).await;
49 tokio::select! {
50 () = tokio::time::sleep(EVERY) => {}
51 _ = stop.wait_for(|stopped| *stopped) => return,
52 }
53 }
54 })
55}
56
57/// One pass over every table. Failures are logged per step.
58pub async fn sweep_once(store: &Store) {
59 let now = now_stamp();
60 match store.sweep_expired_sessions().await {
61 Ok(0) => {}
62 Ok(count) => tracing::info!(count, "sweeper: expired sessions removed"),
63 Err(error) => tracing::error!(error = %error, "sweeper: sessions"),
64 }
65 let codes_before = stamp_minus(&now, CODE_KEEP).unwrap_or_else(|| now.clone());
66 match store.sweep_auth_codes(&codes_before).await {
67 Ok(0) => {}
68 Ok(count) => tracing::info!(count, "sweeper: expired authorization codes removed"),
69 Err(error) => tracing::error!(error = %error, "sweeper: authorization codes"),
70 }
71 match store.sweep_refresh_tokens(&now).await {
72 Ok(0) => {}
73 Ok(count) => tracing::info!(count, "sweeper: expired refresh tokens removed"),
74 Err(error) => tracing::error!(error = %error, "sweeper: refresh tokens"),
75 }
76 match store.sweep_revoked_access_tokens(&now).await {
77 Ok(0) => {}
78 Ok(count) => tracing::info!(count, "sweeper: expired access token revocations removed"),
79 Err(error) => tracing::error!(error = %error, "sweeper: access token revocations"),
80 }
81 match store
82 .prune_directory_changes(&stamp_before(DIRECTORY_KEEP))
83 .await
84 {
85 Ok(0) => {}
86 Ok(count) => tracing::info!(count, "sweeper: old directory changes removed"),
87 Err(error) => tracing::error!(error = %error, "sweeper: directory changes"),
88 }
89 match store.sweep_links(&now).await {
90 Ok(0) => {}
91 Ok(count) => tracing::info!(count, "sweeper: expired account links removed"),
92 Err(error) => tracing::error!(error = %error, "sweeper: account links"),
93 }
94 match store.sweep_invites(&now).await {
95 Ok(0) => {}
96 Ok(count) => tracing::info!(count, "sweeper: expired invitations removed"),
97 Err(error) => tracing::error!(error = %error, "sweeper: invitations"),
98 }
99 match store.prune_sent_mail(&stamp_before(MAIL_KEEP)).await {
100 Ok(0) => {}
101 Ok(count) => tracing::info!(count, "sweeper: sent mail removed"),
102 Err(error) => tracing::error!(error = %error, "sweeper: sent mail"),
103 }
104 match store.prune_failed_mail(&stamp_before(MAIL_KEEP)).await {
105 Ok(0) => {}
106 Ok(count) => tracing::info!(count, "sweeper: failed mail removed"),
107 Err(error) => tracing::error!(error = %error, "sweeper: failed mail"),
108 }
109 match store
110 .prune_backchannel_outbox(&stamp_before(OUTBOX_KEEP))
111 .await
112 {
113 Ok(0) => {}
114 Ok(count) => tracing::info!(count, "sweeper: finished back-channel deliveries removed"),
115 Err(error) => tracing::error!(error = %error, "sweeper: back-channel deliveries"),
116 }
117 match store.prune_audit(&stamp_before(AUDIT_KEEP)).await {
118 Ok(0) => {}
119 Ok(count) => tracing::info!(count, "sweeper: old audit entries removed"),
120 Err(error) => tracing::error!(error = %error, "sweeper: audit entries"),
121 }
122}