Struct DecisionCache

Source
pub struct DecisionCache { /* private fields */ }
Expand description

LRU + TTL decision cache.

Backed by a simple LRU map. Thread-safe via [parking_lot::RwLock]: reads (the common case on a hot PDP) take a shared lock and run concurrently; only put() (cache fill) takes the exclusive lock.

Note: a cache miss can still trigger a “thundering herd” where many concurrent readers all see None for a popular key and all run the underlying authorization. A single-flight pattern (per-key OnceCell) would close this, but is left for a future enhancement.

Implementations§

Source§

impl DecisionCache

Source

pub fn try_new( config: CacheConfig, clock: Arc<dyn Clock>, ) -> Result<Self, String>

Construct a cache after validating its public configuration.

Use this at application boundaries so invalid capacities produce a recoverable configuration error instead of a panic.

Source

pub fn new(config: CacheConfig, clock: Arc<dyn Clock>) -> Self

Construct a cache with an already validated configuration.

§Panics

Panics when the capacity is zero or a TTL exceeds the monotonic clock range. Use Self::try_new when values come from application configuration or another fallible input.

Source

pub fn disabled(clock: Arc<dyn Clock>) -> Self

A disabled cache (every call is a miss).

Source

pub fn policy_version(&self) -> u64

Source

pub fn invalidate_all(&self)

Bump the policy version. All cache entries are invalidated on the next get() because the stored cached_at_policy_version no longer matches.

Source

pub fn set_policy_version(&self, v: u64)

Convenience: set the policy version to a specific value.

Source

pub fn get(&self, key: &CacheKey) -> Option<CachedDecision>

Source

pub fn sweep_stale(&self) -> usize

Drain stale (policy-version mismatch) and expired entries from the cache. Cheap when nothing is stale: O(1) under the fast path. Designed to be called from a background task or after Self::invalidate_all.

Returns the number of entries dropped. Used by tests to assert the eviction worked.

Source

pub fn put(&self, key: CacheKey, decision: CachedDecision)

Source

pub fn stats(&self) -> CacheStats

Source

pub fn config_from_env() -> CacheConfig

👎Deprecated: use try_config_from_env to reject malformed settings

Build a CacheConfig from the AGENTGUARD_CACHE_* environment variables.

Environment knobs (humantime where applicable):

  • AGENTGUARD_CACHE_TTL — sets allow_ttl. Does NOT touch deny_ttl (use AGENTGUARD_DENY_CACHE_TTL for that).
  • AGENTGUARD_DENY_CACHE_TTL — sets deny_ttl independently. Falls back to the default when unset.
  • AGENTGUARD_CACHE_CAPACITY — sets the LRU capacity (positive integer).

Falls back to defaults when unset or unparseable.

Source

pub fn try_config_from_env() -> Result<CacheConfig, String>

Parse cache settings from the process environment, rejecting malformed values rather than silently changing the requested operating limits.

Source

pub fn try_config_from_overrides( cache_ttl: Option<&str>, deny_ttl: Option<&str>, capacity: Option<&str>, ) -> Result<CacheConfig, String>

Parse optional environment-style cache overrides strictly.

Source

pub fn config_from_overrides( cache_ttl: Option<String>, deny_ttl: Option<String>, capacity: Option<String>, ) -> CacheConfig

Same as Self::config_from_env but accepts explicit overrides. Used by tests so they don’t have to mutate std::env from unsafe blocks (the crate forbids unsafe).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,