Skip to main content

InventoryCache

Struct InventoryCache 

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

In-memory inventory cache with a freshness window (design D2).

Owned by the supervisor’s sweep loop: the sweep stores a fresh snapshot at its cadence, and /tell / /agents reuse the cached snapshot while it is younger than max_age, rebuilding on demand only when stale. There is no on-disk cache — a supervisor restart produces a fresh inventory.

Implementations§

Source§

impl InventoryCache

Source

pub fn new(max_age: Duration) -> Self

Creates an empty cache with the given freshness window.

Source

pub fn from_seconds(seconds: u64) -> Self

Creates an empty cache with a freshness window in seconds (the [supervisor.tell] inventory_max_age_seconds config value).

Source

pub fn max_age(&self) -> Duration

The configured freshness window.

Source

pub fn snapshot(&self) -> Option<&AgentInventory>

The cached snapshot, if any.

Source

pub fn is_fresh_at(&self, now: Instant) -> bool

Whether the cache holds a snapshot still within max_age as of now.

Source

pub fn store(&mut self, snapshot: AgentInventory)

Stores a freshly-built snapshot (called by the sweep loop).

Source

pub fn get_or_refresh<F, E>( &mut self, now: Instant, refresh: F, ) -> Result<&AgentInventory, E>
where F: FnOnce() -> Result<AgentInventory, E>,

Returns the cached snapshot when fresh as of now; otherwise calls refresh to rebuild it, stores the result, and returns it.

refresh is invoked at most once per stale lookup, so rapid consecutive /agents within the freshness window trigger only one rebuild (the broker is not re-polled while the snapshot is fresh).

§Errors

Propagates any error from refresh; the previous snapshot (if any) is left untouched on failure.

§Panics

Does not panic in practice: when the cache is stale refresh populates the snapshot before it is unwrapped, so the Some invariant holds.

Trait Implementations§

Source§

impl Debug for InventoryCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

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

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

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, 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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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