Skip to main content

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}
42
43impl Default for StoreAuthConfig {
44    fn default() -> Self {
45        Self {
46            traffic_mode: TrafficRecordingMode::default(),
47            batch_flush_interval: Duration::from_secs(30),
48            batch_max_pending: 1000,
49            cache_enabled: false,
50            cache_ttl: Duration::from_secs(60),
51            neg_cache_ttl: Duration::from_secs(5),
52        }
53    }
54}