trojan_auth/store/config.rs
1//! Configuration for [`StoreAuth`](super::StoreAuth).
2
3use std::time::Duration;
4
5/// How traffic is recorded by [`StoreAuth`](super::StoreAuth).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum TrafficRecordingMode {
8 /// Immediate write on each `record_traffic` call.
9 /// Most accurate but highest backend load.
10 Immediate,
11
12 /// Batch updates at regular intervals via [`TrafficRecorder`](super::TrafficRecorder).
13 /// Better performance, slight delay in traffic accounting.
14 #[default]
15 Batched,
16
17 /// Do not record traffic at all.
18 Disabled,
19}
20
21/// Shared configuration consumed by [`StoreAuth`](super::StoreAuth).
22///
23/// Backend-specific configs (e.g. `SqlAuthConfig`) extract a `StoreAuthConfig`
24/// internally when constructing the `StoreAuth` wrapper.
25#[derive(Debug, Clone)]
26pub struct StoreAuthConfig {
27 /// Traffic recording mode.
28 pub traffic_mode: TrafficRecordingMode,
29 /// Batch flush interval (only used with [`TrafficRecordingMode::Batched`]).
30 pub batch_flush_interval: Duration,
31 /// Maximum pending traffic updates before a forced flush.
32 pub batch_max_pending: usize,
33 /// Whether to enable authentication result caching.
34 pub cache_enabled: bool,
35 /// Positive cache entry TTL.
36 pub cache_ttl: Duration,
37 /// Negative cache entry TTL (invalid-hash rejection cache).
38 ///
39 /// Set to `Duration::ZERO` to disable negative caching.
40 pub neg_cache_ttl: Duration,
41 /// Stale-while-revalidate window beyond the positive cache TTL.
42 ///
43 /// When a cache entry is past `cache_ttl` but within `cache_ttl + stale_ttl`,
44 /// the cached result is returned immediately while a background revalidation
45 /// is spawned. Set to `Duration::ZERO` to disable (default).
46 pub stale_ttl: Duration,
47}
48
49impl Default for StoreAuthConfig {
50 fn default() -> Self {
51 Self {
52 traffic_mode: TrafficRecordingMode::default(),
53 batch_flush_interval: Duration::from_secs(30),
54 batch_max_pending: 1000,
55 cache_enabled: false,
56 cache_ttl: Duration::from_secs(60),
57 neg_cache_ttl: Duration::from_secs(5),
58 stale_ttl: Duration::ZERO,
59 }
60 }
61}