1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use crate::{FactKey, FactLoadError, FactLoadResult, FactSource, FactSourceRegistrationError};
use futures_channel::oneshot;
use std::any::{Any, TypeId};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::Hasher;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use tracing::Instrument;
const FACT_STATE_STRIPES: usize = 64;
#[derive(Default)]
struct EvaluationSessionInner {
states: Mutex<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
next_load_id: AtomicU64,
shared_empty: bool,
}
struct FactState<K>
where
K: FactKey,
{
source: Mutex<Option<Arc<dyn FactSource<K>>>>,
stripes: Box<[Mutex<FactStripe<K>>]>,
}
struct FactStripe<K>
where
K: FactKey,
{
cache: HashMap<K, FactLoadResult<K::Value>>,
in_flight: HashMap<K, Vec<oneshot::Sender<()>>>,
}
impl<K> Default for FactStripe<K>
where
K: FactKey,
{
fn default() -> Self {
Self {
cache: HashMap::new(),
in_flight: HashMap::new(),
}
}
}
impl<K> FactState<K>
where
K: FactKey,
{
fn new(source: Option<Arc<dyn FactSource<K>>>) -> Self {
let stripes = (0..FACT_STATE_STRIPES)
.map(|_| Mutex::new(FactStripe::default()))
.collect();
Self {
source: Mutex::new(source),
stripes,
}
}
fn insert_source(
&self,
source: Arc<dyn FactSource<K>>,
replace: bool,
) -> Result<(), FactSourceRegistrationError> {
let mut source_guard = self
.source
.lock()
.expect("fact source mutex should not be poisoned");
let mut stripes = self.lock_stripes();
if !replace && source_guard.is_some() {
return Err(FactSourceRegistrationError::AlreadyRegistered { fact_name: K::NAME });
}
if stripes.iter().any(|stripe| !stripe.in_flight.is_empty()) {
return Err(FactSourceRegistrationError::InFlight { fact_name: K::NAME });
}
*source_guard = Some(source);
for stripe in &mut stripes {
stripe.cache.clear();
}
Ok(())
}
fn lock_stripes(&self) -> Vec<MutexGuard<'_, FactStripe<K>>> {
self.stripes
.iter()
.map(|stripe| {
stripe
.lock()
.expect("fact state stripe mutex should not be poisoned")
})
.collect()
}
fn stripe_index(&self, key: &K) -> usize {
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
(hasher.finish() as usize) % self.stripes.len()
}
fn plan_loads(&self, keys: &[K]) -> LoadPlan<K> {
let key_stripes = keys
.iter()
.map(|key| self.stripe_index(key))
.collect::<Vec<_>>();
let mut seen = HashSet::new();
let unique_keys = keys
.iter()
.zip(key_stripes.iter().copied())
.filter(|(key, _)| seen.insert((*key).clone()))
.map(|(key, stripe_index)| (key.clone(), stripe_index))
.collect::<Vec<_>>();
let source_guard = self
.source
.lock()
.expect("fact source mutex should not be poisoned");
let source = source_guard.clone();
let cached_results = keys
.iter()
.zip(key_stripes.iter().copied())
.map(|(key, stripe_index)| {
let stripe = self.stripes[stripe_index]
.lock()
.expect("fact state stripe mutex should not be poisoned");
stripe.cache.get(key).cloned()
})
.collect::<Option<Vec<_>>>();
if let Some(results) = cached_results {
return LoadPlan {
source,
cached_results: Some(results),
keys: Vec::new(),
waiters: Vec::new(),
};
}
let mut missing = Vec::new();
let mut waiters = Vec::new();
for (key, stripe_index) in unique_keys {
let mut stripe = self.stripes[stripe_index]
.lock()
.expect("fact state stripe mutex should not be poisoned");
if stripe.cache.contains_key(&key) {
continue;
}
if let Some(existing_waiters) = stripe.in_flight.get_mut(&key) {
let (sender, receiver) = oneshot::channel();
existing_waiters.push(sender);
waiters.push(receiver);
} else {
stripe.in_flight.insert(key.clone(), Vec::new());
missing.push(key.clone());
}
}
LoadPlan {
source,
cached_results: None,
keys: missing,
waiters,
}
}
fn finish_in_flight(&self, keys: &[K]) {
let mut waiters = Vec::new();
for key in keys {
let stripe_index = self.stripe_index(key);
let mut stripe = self.stripes[stripe_index]
.lock()
.expect("fact state stripe mutex should not be poisoned");
if let Some(key_waiters) = stripe.in_flight.remove(key) {
waiters.extend(key_waiters);
}
}
for waiter in waiters {
let _ = waiter.send(());
}
}
fn cache_loaded(&self, keys: &[K], results: Vec<FactLoadResult<K::Value>>) {
for (key, result) in keys.iter().cloned().zip(results) {
let stripe_index = self.stripe_index(&key);
let mut stripe = self.stripes[stripe_index]
.lock()
.expect("fact state stripe mutex should not be poisoned");
stripe.cache.insert(key, result);
}
}
fn results_from_cache(&self, keys: &[K]) -> Vec<FactLoadResult<K::Value>> {
keys.iter()
.map(|key| {
let stripe_index = self.stripe_index(key);
let stripe = self.stripes[stripe_index]
.lock()
.expect("fact state stripe mutex should not be poisoned");
stripe.cache.get(key).cloned().unwrap_or_else(|| {
FactLoadResult::Error(FactLoadError::SourceContractViolation {
fact_name: K::NAME,
expected: 1,
actual: 0,
})
})
})
.collect()
}
}
/// Request-scoped fact loading and caching state.
///
/// A session is intended to live for one request or one authorization pass. It
/// owns registered fact sources and caches loaded facts by key type. The cache
/// is deliberately not process-global. Cached facts and cached errors are
/// dropped with the session, so permission revocations or backend changes are
/// observed by the next request's session rather than being held process-wide.
#[derive(Clone, Default)]
pub struct EvaluationSession {
inner: Arc<EvaluationSessionInner>,
}
impl EvaluationSession {
/// Creates an empty request-scoped session.
pub fn new() -> Self {
Self::default()
}
/// Creates an explicitly empty request-scoped session.
///
/// This is equivalent to [`Self::new`]. It can make call sites clearer when
/// only RBAC/ABAC policies are expected and no fact sources are registered.
/// For very hot RBAC/ABAC-only loops, use [`Self::shared_empty`] to avoid
/// allocating a new empty session per call.
pub fn empty() -> Self {
Self::new()
}
/// Returns a process-wide empty session for hot paths that never use fact
/// sources.
///
/// This avoids allocating a new empty session for RBAC/ABAC-only checks in
/// tight loops such as fanout or SSE frame authorization. It is only safe
/// when no fact-backed policies are expected. Calling [`Self::register`],
/// [`Self::register_arc`], [`Self::replace`], or [`Self::replace_arc`] on
/// this session will panic.
pub fn shared_empty() -> &'static Self {
static SHARED_EMPTY: OnceLock<EvaluationSession> = OnceLock::new();
SHARED_EMPTY.get_or_init(|| EvaluationSession {
inner: Arc::new(EvaluationSessionInner {
shared_empty: true,
..EvaluationSessionInner::default()
}),
})
}
/// Starts building a request-scoped session with all fact sources declared
/// in one place.
pub fn builder() -> EvaluationSessionBuilder {
EvaluationSessionBuilder::new()
}
/// Registers a fact source for one key type.
///
/// Panics if a source for `K` is already registered. Use [`Self::replace`]
/// when replacing a source is intentional. Register sources during session
/// setup; registering while loads for the same key type are in flight is
/// not a supported operation and will panic.
pub fn register<K, S>(&self, source: S)
where
K: FactKey,
S: FactSource<K> + 'static,
{
self.register_arc::<K>(Arc::new(source));
}
/// Registers a shared fact source for one key type.
///
/// Panics if a source for `K` is already registered. Register sources
/// during session setup; use [`Self::replace_arc`] only when overwriting is
/// deliberate. Registering while loads for the same key type are in flight
/// is not a supported operation and will panic.
///
/// The registry is keyed by the exact Rust fact key type. If two production
/// backends serve the same logical shape, such as the same
/// `RelationshipQuery<UserId, ConversationId, ParticipantRelation>`, they
/// cannot both be registered under that exact type in one session. Wrap one
/// ID/relation type or define distinct fact keys so each backend has a
/// separate registry entry.
pub fn register_arc<K>(&self, source: Arc<dyn FactSource<K>>)
where
K: FactKey,
{
self.try_register_arc::<K>(source)
.unwrap_or_else(|error| panic!("{error}"));
}
/// Registers a fact source for one key type, returning an error instead of
/// panicking if registration is invalid.
pub fn try_register<K, S>(&self, source: S) -> Result<(), FactSourceRegistrationError>
where
K: FactKey,
S: FactSource<K> + 'static,
{
self.try_register_arc::<K>(Arc::new(source))
}
/// Registers a shared fact source for one key type, returning an error
/// instead of panicking if registration is invalid.
pub fn try_register_arc<K>(
&self,
source: Arc<dyn FactSource<K>>,
) -> Result<(), FactSourceRegistrationError>
where
K: FactKey,
{
self.insert_source::<K>(source, false)
}
/// Explicitly replaces a fact source for one key type.
///
/// Replacing a source clears any cached facts for that key type in this
/// session. Replacing while loads for the same key type are in flight is
/// not a supported operation and will panic.
pub fn replace<K, S>(&self, source: S)
where
K: FactKey,
S: FactSource<K> + 'static,
{
self.replace_arc::<K>(Arc::new(source));
}
/// Explicitly replaces a shared fact source for one key type.
///
/// Replacing a source clears any cached facts for that key type in this
/// session. Replacing while loads for the same key type are in flight is
/// not a supported operation and will panic.
pub fn replace_arc<K>(&self, source: Arc<dyn FactSource<K>>)
where
K: FactKey,
{
self.try_replace_arc::<K>(source)
.unwrap_or_else(|error| panic!("{error}"));
}
/// Explicitly replaces a fact source for one key type, returning an error
/// instead of panicking if replacement is invalid.
pub fn try_replace<K, S>(&self, source: S) -> Result<(), FactSourceRegistrationError>
where
K: FactKey,
S: FactSource<K> + 'static,
{
self.try_replace_arc::<K>(Arc::new(source))
}
/// Explicitly replaces a shared fact source for one key type, returning an
/// error instead of panicking if replacement is invalid.
pub fn try_replace_arc<K>(
&self,
source: Arc<dyn FactSource<K>>,
) -> Result<(), FactSourceRegistrationError>
where
K: FactKey,
{
self.insert_source::<K>(source, true)
}
fn insert_source<K>(
&self,
source: Arc<dyn FactSource<K>>,
replace: bool,
) -> Result<(), FactSourceRegistrationError>
where
K: FactKey,
{
if self.inner.shared_empty {
return Err(FactSourceRegistrationError::SharedEmptySession { fact_name: K::NAME });
}
let type_id = TypeId::of::<K>();
let state = {
let mut states = self
.inner
.states
.lock()
.expect("fact state registry mutex should not be poisoned");
if let Some(existing) = states
.get(&type_id)
.and_then(|state| state.downcast_ref::<Arc<FactState<K>>>())
{
Arc::clone(existing)
} else {
let state = Arc::new(FactState::new(None));
states.insert(type_id, Box::new(Arc::clone(&state)));
state
}
};
state.insert_source(source, replace)
}
/// Loads one fact through the session cache.
pub async fn get<K>(&self, key: K) -> FactLoadResult<K::Value>
where
K: FactKey,
{
self.get_many(&[key])
.await
.into_iter()
.next()
.unwrap_or_else(|| {
FactLoadResult::Error(FactLoadError::SourceContractViolation {
fact_name: K::NAME,
expected: 1,
actual: 0,
})
})
}
/// Loads facts through the session cache.
///
/// Results preserve input order and duplicate keys. Missing cache entries
/// are deduplicated before they are loaded, then chunked according to the
/// source's [`FactSource::max_batch_size`] hint.
pub async fn get_many<K>(&self, keys: &[K]) -> Vec<FactLoadResult<K::Value>>
where
K: FactKey,
{
if keys.is_empty() {
return Vec::new();
}
let state = self.state::<K>();
let load_plan = state.plan_loads(keys);
if let Some(results) = load_plan.cached_results {
return results;
}
let mut in_flight_guard = InFlightGuard::new(Arc::clone(&state), load_plan.keys.clone());
if !load_plan.keys.is_empty() {
if let Some(source) = load_plan.source.as_ref() {
let chunk_size = source
.max_batch_size()
.map_or(load_plan.keys.len(), NonZeroUsize::get)
.max(1);
for chunk in load_plan.keys.chunks(chunk_size) {
let load_id = self.inner.next_load_id.fetch_add(1, Ordering::Relaxed);
let load_span = tracing::debug_span!(
"gatehouse.fact_load",
fact.name = K::NAME,
fact.load_id = load_id,
fact.key_count = chunk.len(),
fact.unique_key_count = chunk.len(),
);
let loaded = source.load_many(chunk).instrument(load_span).await;
if loaded.len() == chunk.len() {
state.cache_loaded(chunk, loaded);
} else {
state.cache_loaded(
chunk,
chunk
.iter()
.map(|_| {
FactLoadResult::Error(FactLoadError::SourceContractViolation {
fact_name: K::NAME,
expected: chunk.len(),
actual: loaded.len(),
})
})
.collect(),
);
}
// Keep this immediately after the cache write, with no await in between.
// The drop guard treats remaining keys as cancelled.
in_flight_guard.finish(chunk);
}
} else {
state.cache_loaded(
&load_plan.keys,
load_plan
.keys
.iter()
.map(|_| {
FactLoadResult::Error(FactLoadError::SourceNotRegistered {
fact_name: K::NAME,
})
})
.collect(),
);
in_flight_guard.finish(&load_plan.keys);
}
}
for waiter in load_plan.waiters {
let _ = waiter.await;
}
state.results_from_cache(keys)
}
fn state<K>(&self) -> Arc<FactState<K>>
where
K: FactKey,
{
let mut states = self
.inner
.states
.lock()
.expect("fact state registry mutex should not be poisoned");
states
.entry(TypeId::of::<K>())
.or_insert_with(|| Box::new(Arc::new(FactState::<K>::new(None))))
.downcast_ref::<Arc<FactState<K>>>()
.expect("fact state type should match registry key")
.clone()
}
}
/// Builder for declaring all fact sources needed by a request-scoped
/// [`EvaluationSession`].
pub struct EvaluationSessionBuilder {
session: EvaluationSession,
}
impl EvaluationSessionBuilder {
fn new() -> Self {
Self {
session: EvaluationSession::new(),
}
}
/// Registers a source for one fact key type.
///
/// Panics if the same fact key type is registered twice.
pub fn with<K, S>(self, source: S) -> Self
where
K: FactKey,
S: FactSource<K> + 'static,
{
self.session.register::<K, _>(source);
self
}
/// Registers a shared source for one fact key type.
///
/// Panics if the same fact key type is registered twice.
pub fn with_arc<K>(self, source: Arc<dyn FactSource<K>>) -> Self
where
K: FactKey,
{
self.session.register_arc::<K>(source);
self
}
/// Finishes the session.
pub fn build(self) -> EvaluationSession {
self.session
}
}
struct LoadPlan<K>
where
K: FactKey,
{
source: Option<Arc<dyn FactSource<K>>>,
cached_results: Option<Vec<FactLoadResult<K::Value>>>,
keys: Vec<K>,
waiters: Vec<oneshot::Receiver<()>>,
}
struct InFlightGuard<K>
where
K: FactKey,
{
state: Arc<FactState<K>>,
remaining: Vec<K>,
}
impl<K> InFlightGuard<K>
where
K: FactKey,
{
fn new(state: Arc<FactState<K>>, keys: Vec<K>) -> Self {
Self {
state,
remaining: keys,
}
}
fn finish(&mut self, keys: &[K]) {
self.state.finish_in_flight(keys);
if self.remaining.is_empty() {
return;
}
let finished = keys.iter().cloned().collect::<HashSet<_>>();
self.remaining.retain(|key| !finished.contains(key));
}
}
impl<K> Drop for InFlightGuard<K>
where
K: FactKey,
{
fn drop(&mut self) {
if self.remaining.is_empty() {
return;
}
self.state.cache_loaded(
&self.remaining,
self.remaining
.iter()
.map(|_| {
FactLoadResult::Error(FactLoadError::LoaderCancelled { fact_name: K::NAME })
})
.collect(),
);
self.state.finish_in_flight(&self.remaining);
}
}