vector_core/community/cache.rs
1//! Per-account RAM cache for Community sync state.
2//!
3//! Holds the page cursors (oldest back-paging floor + newest `since` floor), history-start flags,
4//! in-flight page de-dup, and the invite preload. All of it is keyed by this account's channel
5//! ids, so it lives on the account's session and goes when that does — there is no invalidation
6//! step to get right.
7
8use nostr_sdk::prelude::Event;
9use std::collections::{HashMap, HashSet};
10use std::sync::Mutex;
11use std::time::Duration;
12use web_time::Instant;
13
14#[derive(Default)]
15struct CommunityCache {
16 /// In-flight page fetches, keyed `"{channel_id}:{older|latest}"`. Anti-stampede: an eager
17 /// user scrolling/clicking can't fire the same page twice — the duplicate no-ops.
18 inflight: HashSet<String>,
19 /// Channels whose network history-start has been reached (an older-page fetch found nothing
20 /// strictly older than the cursor). Older-page requests for these go DB-only.
21 history_start: HashSet<String>,
22 /// Oldest OUTER (wire send-time) created_at, in seconds, fetched per channel. The relay
23 /// filters `until` against the outer created_at, so the back-paging cursor MUST be on that
24 /// clock — not the inner authored `at`, which a hostile member can backdate/post-date.
25 oldest_cursor: HashMap<String, u64>,
26 /// Newest OUTER created_at (seconds) seen on a LATEST-page fetch per channel. Used as `since`
27 /// on the next latest fetch so a routine re-sync returns only genuinely-new events instead of
28 /// re-downloading + re-decrypting the same newest page. Advanced ONLY by latest fetches (never
29 /// older pages) — it means "nothing newer than this needs a top-fetch"; any below-page gap is
30 /// a back-pagination concern, not a top-fetch one.
31 newest_cursor: HashMap<String, u64>,
32}
33
34struct CacheKey;
35
36/// Run `f` against this account's cache.
37///
38/// A closure rather than a returned guard, so the lock provably cannot be held
39/// across an await: this is pure optimisation state and every use is a lookup
40/// or an insert. Poison-tolerant for the same reason — a panicking caller must
41/// not cascade into every future community sync.
42fn with_cache<R>(f: impl FnOnce(&mut CommunityCache) -> R) -> R {
43 let cache = crate::db::current_session().scoped::<CacheKey, Mutex<CommunityCache>>();
44 let mut guard = cache.lock().unwrap_or_else(|e| e.into_inner());
45 f(&mut guard)
46}
47
48/// Claim an in-flight page fetch (key `"{channel_id}:{older|latest}"`). Returns `false` if one is
49/// already running — the caller should no-op. Pair with [`end_page_fetch`].
50pub fn try_begin_page_fetch(key: &str) -> bool {
51 with_cache(|c| c.inflight.insert(key.to_string()))
52}
53
54/// Release an in-flight page-fetch claim (success or error).
55pub fn end_page_fetch(key: &str) {
56 with_cache(|c| c.inflight.remove(key));
57}
58
59/// Has the channel's network history-start been reached? Older pages then stay DB-only.
60pub fn is_at_history_start(channel_id: &str) -> bool {
61 with_cache(|c| c.history_start.contains(channel_id))
62}
63
64/// Mark the channel as having reached its network history-start.
65pub fn mark_history_start(channel_id: &str) {
66 with_cache(|c| c.history_start.insert(channel_id.to_string()));
67}
68
69/// Oldest OUTER created_at (seconds) fetched for the channel — the back-paging cursor.
70pub fn oldest_cursor(channel_id: &str) -> Option<u64> {
71 with_cache(|c| c.oldest_cursor.get(channel_id).copied())
72}
73
74/// Advance the back-paging cursor to the oldest wire time this page returned (monotonic — only
75/// ever steps further back).
76pub fn advance_oldest_cursor(channel_id: &str, oldest_secs: u64) {
77 with_cache(|c| {
78 let slot = c.oldest_cursor.entry(channel_id.to_string()).or_insert(oldest_secs);
79 *slot = (*slot).min(oldest_secs);
80 });
81}
82
83/// Newest OUTER created_at (seconds) seen on a latest page for the channel — the `since` floor
84/// for the next latest fetch. `None` before the first latest fetch this session (→ full newest page).
85pub fn newest_cursor(channel_id: &str) -> Option<u64> {
86 with_cache(|c| c.newest_cursor.get(channel_id).copied())
87}
88
89/// Advance the latest-page `since` floor to the newest wire time this page returned (monotonic —
90/// only ever steps forward). Call ONLY for latest-page fetches.
91pub fn advance_newest_cursor(channel_id: &str, newest_secs: u64) {
92 with_cache(|c| {
93 let slot = c.newest_cursor.entry(channel_id.to_string()).or_insert(newest_secs);
94 *slot = (*slot).max(newest_secs);
95 });
96}
97
98/// Clear a channel's back-paging floors (history-start + oldest cursor) — e.g. after a
99/// multi-epoch backfill makes older history reachable again.
100pub fn clear_channel_floors(channel_id: &str) {
101 with_cache(|c| {
102 c.history_start.remove(channel_id);
103 c.oldest_cursor.remove(channel_id);
104 });
105}
106
107/// Drop ALL of a channel's sync state (floors + the latest-page `since` cursor) — community
108/// teardown. A surviving `since` cursor makes a same-session REJOIN sync "since I left"
109/// instead of cold, so the rejoined chat opens empty despite plenty of history.
110pub fn clear_channel_sync_state(channel_id: &str) {
111 with_cache(|c| {
112 c.history_start.remove(channel_id);
113 c.oldest_cursor.remove(channel_id);
114 c.newest_cursor.remove(channel_id);
115 });
116}
117
118// ── Invite preload ──────────────────────────────────────────────────────────
119// Warmed-ahead-of-Join state: the primary channel's first page, fetched at invite-receive /
120// public-preview time so a Join can open to a populated chat instead of a ~10s sync. RAM-only —
121// nothing is persisted for a community the user hasn't joined, so a declined invite leaves no DB
122// trace. Session-scoped + TTL'd + capped.
123
124/// How long a warmed page stays promotable. Past this, Join falls back to a normal sync.
125pub(crate) const PRELOAD_TTL: Duration = Duration::from_secs(120);
126/// Max communities warmed at once (bounds memory; oldest evicted on overflow).
127const PRELOAD_MAX: usize = 8;
128
129/// How long the sync will adopt an in-flight (Pending) preload before giving up and fetching itself.
130/// Generous: the preload fetch is itself relay-racing, so adopting it is never slower than firing a
131/// parallel fetch — and a failed preload aborts (→ absent) so the sync falls back immediately, not
132/// at the deadline.
133const PRELOAD_ADOPT_TIMEOUT: Duration = Duration::from_secs(12);
134
135enum PreloadState {
136 /// A warm-up fetch is in flight. A Join can ADOPT it (await this result) instead of firing its
137 /// own — so the speedup holds even when the user taps Join before the warm-up finished.
138 Pending,
139 /// The warmed page is ready to promote/adopt.
140 Ready(Vec<Event>),
141}
142
143struct Preload {
144 state: PreloadState,
145 fetched_at: Instant,
146}
147
148struct PreloadKey;
149
150fn with_preload<R>(f: impl FnOnce(&mut HashMap<String, Preload>) -> R) -> R {
151 let map = crate::db::current_session().scoped::<PreloadKey, Mutex<HashMap<String, Preload>>>();
152 let mut guard = map.lock().unwrap_or_else(|e| e.into_inner());
153 f(&mut guard)
154}
155
156/// Mark a community's warm-up as in-flight (so a racing Join adopts it rather than double-fetching).
157/// Evicts expired entries and, if over the cap, the oldest.
158pub fn begin_preload(community_id: &str) {
159 with_preload(|map| {
160 map.retain(|_, p| p.fetched_at.elapsed() < PRELOAD_TTL);
161 if map.len() >= PRELOAD_MAX {
162 if let Some(oldest) = map.iter().min_by_key(|(_, p)| p.fetched_at).map(|(k, _)| k.clone()) {
163 map.remove(&oldest);
164 }
165 }
166 map.insert(
167 community_id.to_string(),
168 Preload { state: PreloadState::Pending, fetched_at: Instant::now() },
169 );
170 });
171}
172
173/// The warm-up fetch landed — make its page available to promote/adopt.
174pub fn finish_preload(community_id: &str, page: Vec<Event>) {
175 with_preload(|map| {
176 if let Some(p) = map.get_mut(community_id) {
177 p.state = PreloadState::Ready(page);
178 p.fetched_at = Instant::now();
179 }
180 });
181}
182
183/// The warm-up fetch failed/was cancelled — drop the entry so an adopter falls back immediately.
184pub fn abort_preload(community_id: &str) {
185 with_preload(|map| map.remove(community_id));
186}
187
188/// Non-blocking take for promotion at Accept: returns the page ONLY if already Ready, leaving a
189/// still-Pending warm-up in place for the sync to adopt. `None` if absent / Pending / stale.
190pub fn take_ready_preload(community_id: &str) -> Option<Vec<Event>> {
191 with_preload(|map| {
192 let fresh = matches!(map.get(community_id), Some(p)
193 if p.fetched_at.elapsed() < PRELOAD_TTL && matches!(p.state, PreloadState::Ready(_)));
194 if !fresh {
195 return None;
196 }
197 match map.remove(community_id) {
198 Some(Preload { state: PreloadState::Ready(page), .. }) => Some(page),
199 _ => None,
200 }
201 })
202}
203
204/// Adopt a community's warm-up as this sync's page: Ready → take it; Pending → await it (the
205/// in-flight fetch IS the page, so this waits only the request's remaining time, never firing a
206/// second); absent/stale/failed → `None` so the caller fetches normally. Polls at coarse granularity
207/// (imperceptible vs. a fresh round-trip) to stay free of notification races.
208pub async fn take_or_await_preload(community_id: &str) -> Option<Vec<Event>> {
209 let deadline = Instant::now() + PRELOAD_ADOPT_TIMEOUT;
210 loop {
211 let adopted = with_preload(|map| match map.get(community_id) {
212 Some(p) if p.fetched_at.elapsed() < PRELOAD_TTL => {
213 if matches!(p.state, PreloadState::Ready(_)) {
214 return match map.remove(community_id) {
215 Some(Preload { state: PreloadState::Ready(page), .. }) => Some(Some(page)),
216 _ => Some(None),
217 };
218 }
219 None // Pending → keep waiting.
220 }
221 _ => Some(None), // absent / stale / aborted → fetch normally
222 });
223 if let Some(outcome) = adopted {
224 return outcome;
225 }
226 if Instant::now() >= deadline {
227 return None;
228 }
229 crate::rt::time::sleep(Duration::from_millis(50)).await;
230 }
231}
232
233/// Drop all cached state. A swap needs no call: this belongs to the account's
234/// session and goes with it. Kept for callers that want a cold cache within one
235/// account (tests, an explicit resync).
236pub fn clear() {
237 with_preload(|map| map.clear());
238 with_cache(|c| *c = CommunityCache::default());
239}