Skip to main content

CachingCardResolver

Struct CachingCardResolver 

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

A caching agent card resolver: a private HTTP cache (RFC 9111) of one agent card (spec §8.6.2: clients SHOULD honor RFC 9111 when fetching Agent Cards).

resolve returns the stored card without a request while it is fresh: for its Cache-Control: max-age, or else its Expires, less the age it arrived with (Age, Date). A stale card is revalidated with If-None-Match / If-Modified-Since, and a card marked no-cache or Vary: * is revalidated on every resolve. A no-store card is not stored. Directives for shared caches (s-maxage, proxy-revalidate) do not apply to it.

A card with no max-age and no Expires is revalidated on every resolve unless with_default_freshness gives it a lifetime (with no ETag or Last-Modified either, that is a full fetch each time, and the card is kept only to be served stale, below), and with_always_revalidate revalidates even a fresh card, so a card the agent changes is seen at once.

§At fleet scale (ADR 0051)

  • A resolver builds its HTTP client once, so a revalidation reuses the connection of the fetch before it; with_pool uses an HttpPool’s connections instead.
  • While a fetch is running, other resolve calls on the resolver and its clones wait for it and share its outcome: N callers holding a stale card make one request.
  • A revalidation that fails because the agent cannot be reached, timed out, or answered 500/502/503/504 returns the stored card if it went stale no longer ago than the stale-if-error window (RFC 5861): the larger of with_stale_if_error and the card’s own Cache-Control: stale-if-error. A card marked must-revalidate or no-cache is never returned stale. A card with no lifetime is stale from the moment it arrives, so the window counts from then, and it is kept (when it has no validator either) only if a window can apply to it.
  • with_refresh_jitter spreads the revalidations of processes that fetched together.

A CardCache holds one resolver per URL for many peers.

Clones share one cache. A resolver fetches the public card only, with the same request headers every time; with_options starts a new cache, so a card stored under one credential is never returned for another.

Implementations§

Source§

impl CachingCardResolver

Source

pub fn new(base_url: &str) -> Result<CachingCardResolver, ClientError>

Creates a new resolver for the given agent card URL.

§Errors

Returns ClientError::InvalidEndpoint if base_url is malformed (empty, missing scheme, etc.).

Source

pub fn with_path( base_url: &str, path: &str, ) -> Result<CachingCardResolver, ClientError>

Creates a new resolver with a custom path.

§Errors

Returns ClientError::InvalidEndpoint if base_url is malformed.

Source

pub fn with_options(self, options: CardFetchOptions) -> CachingCardResolver

Fetches with options: their headers and budget, and, with signing, their signature keys, so a card that does not verify is never cached or returned (spec §14: clients SHOULD verify signatures when present). Default: CardFetchOptions::default.

Starts a new, empty cache, with no extended card installed. A card stored for other request headers, another credential’s, is not a response to this resolver’s requests (RFC 9111 §4.1), and the cache it would otherwise keep is shared with every clone.

Source

pub fn with_pool(self, pool: &HttpPool) -> CachingCardResolver

Available on crate feature tls-rustls only.

Fetches through pool’s connections (ADR 0032 decision 6), shared with every client and resolver given the same pool. The cache is kept: the connection a response arrived on does not change it.

A pool has one TLS configuration, so options that carry their own (CardFetchOptions::with_tls_config) conflict with it: every resolve then fails with ClientError::Config naming both, rather than ignoring one.

Source

pub const fn with_always_revalidate(self, always: bool) -> CachingCardResolver

Whether every resolve revalidates the stored card, however long the server said it stays fresh. Default: false.

true is how the resolver behaved before 0.15: a request on every resolve, a 304 when the card is unchanged. It suits an agent whose card changes without notice (a hot-reloaded card served with a long max-age), at a round trip per resolve. RFC 9111 allows a cache to validate a fresh response at any time.

Source

pub const fn with_default_freshness( self, lifetime: Duration, ) -> CachingCardResolver

How long a card stays fresh when its response says nothing about it: no max-age and no Expires (spec §8.6.2: when the server does not include caching headers, clients MAY apply an implementation-specific default cache duration). Default: none, so such a card is revalidated on every resolve.

The lifetime counts from when the card arrived, less any Age it arrived with, and is capped at 2^31 seconds (RFC 9111 §1.2.2). It is never applied to a card marked no-cache or no-store.

Source

pub const fn with_stale_if_error(self, window: Duration) -> CachingCardResolver

How long after it went stale the stored card may still be returned when its revalidation fails because the agent could not be reached, timed out, or answered 500, 502, 503 or 504 (RFC 5861 §4). Default: none, so only the card’s own stale-if-error directive allows it.

The window used is the larger of this and that directive. A card marked must-revalidate or no-cache is never returned stale, and a failure of another kind (a 404, a card that does not parse or verify) is returned as the error it is.

Source

pub const fn with_refresh_jitter(self, fraction: f64) -> CachingCardResolver

Shortens each stored card’s freshness by a random share of up to fraction of its lifetime, drawn when the card is stored, so that processes which fetched a card together revalidate it apart. Never lengthens it. fraction is clamped to 0.0..=0.5; not a number is 0.0. Default: 0.0 (a CardCache uses 0.1).

Source

pub async fn resolve(&self) -> Result<AgentCard, ClientError>

Resolves the agent card, using the stored one while it is fresh.

Returns the stored card without a request while it is fresh. Otherwise fetches, with conditional request headers when a card is stored: on 304, returns the stored card, now as fresh as the 304 says; on 200, stores and returns the new card. Concurrent calls share one fetch, and a fetch that fails for want of the agent may return the stored card (see with_stale_if_error).

§Errors

Same conditions as resolve_agent_card, and ClientError::Config when with_pool and options with their own TLS configuration were both given.

Source

pub async fn invalidate(&self)

Clears the internal cache, the extended card included.

Source

pub async fn install_extended_card(&self, card: AgentCard)

Uses card, the extended card from GetExtendedAgentCard, in place of the public one until clear_extended_card (spec §13.3: clients SHOULD replace their cached public card with the extended version for the duration of their authenticated session). While it is installed, resolve returns it without fetching.

Source

pub async fn clear_extended_card(&self)

Ends the authenticated session’s view: resolve goes back to the public card.

Source

pub fn url(&self) -> &str

The card URL this resolver fetches.

Source

pub fn shares_cache_with(&self, other: &CachingCardResolver) -> bool

Whether self and other share one cache (one is a clone of the other, or both came from one CardCache for one URL).

Trait Implementations§

Source§

impl Clone for CachingCardResolver

Source§

fn clone(&self) -> CachingCardResolver

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CachingCardResolver

Source§

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

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

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,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

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
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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