Skip to main content

SourceCache

Struct SourceCache 

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

A thread-safe cache of complete host result sets in one finder’s scope.

A host can publish several feeds. Self::find_sources_by_host retains every distinct matching descriptor observed over its discovery window, rather than choosing a feed by arrival order. Use Finder::find_source for a selection that should return as soon as a particular feed appears.

The cache owns its configured finder, keeping it and the NDI runtime alive. Configure groups, IP hints and Advanced settings before constructing the cache: a lookup never creates another finder or changes its configuration. Store it directly in server state, or share it through std::sync::Arc. finder lends the configured finder for uncached selection.

Entries remain until Self::invalidate or Self::clear; they can contain offline endpoints and omit feeds announced after the original window. Empty results are not cached. A miss with a busy final snapshot returns the feeds observed so far without filling the cache, so the next miss tries discovery again. A failed cache reservation returns the discovered result uncached. See Self::find_sources_by_host for allocation behavior during publication. The accepted grammar is Source::matches_host’s. Equivalent hostname or machine-name ASCII case/final-dot and numeric IP spellings share an entry. Other spellings, including DNS aliases, remain distinct identities.

The cache is Send + Sync. Discovery, descriptor copies and retired entry destruction run outside its mutex so unrelated lookups and invalidation remain available; SDK snapshots retain their normal per-finder serialization. Returned vectors own copies of the descriptors. Concurrent discovery waits share change reports through that finder, so one lookup cannot consume another lookup’s notification. Dropping the cache destroys its native finder and releases its runtime handle synchronously on the dropping thread.

let ndi = Ndi::new()?;
let finder = Finder::new(&ndi, &FinderOptions::new().groups(["Studio"])
    .extra_ips(["192.168.0.107"]))?;
let cache = SourceCache::new(finder);
let feeds = cache.find_sources_by_host("192.168.0.107", Duration::from_secs(5))?;
for feed in feeds { println!("{feed}"); }

Implementations§

Source§

impl SourceCache

Source

pub fn new(finder: Finder) -> Self

Create an empty cache owning finder and its discovery configuration.

This moves the finder without allocating, cloning or making an SDK call. It keeps the finder alive and NDI initialized until the cache is dropped. Share the cache through std::sync::Arc for long-lived server workers; finder reaches the same finder for uncached lookups.

use grafton_ndi::{Finder, FinderOptions, Ndi, Result, SourceCache};
use std::sync::Arc;

struct ServerState {
    sources: SourceCache,
}

fn server_state(ndi: &Ndi) -> Result<Arc<ServerState>> {
    let options = FinderOptions::new().groups(["Studio"]);
    let finder = Finder::new(ndi, &options)?;
    Ok(Arc::new(ServerState {
        sources: SourceCache::new(finder),
    }))
}
Source

pub fn finder(&self) -> &Finder

The configured finder, borrowed for uncached discovery and selection.

This allocates nothing and makes no SDK call. The finder cannot be replaced through this borrow, so cached entries retain their scope.

Source

pub fn find_sources_by_host( &self, host: &str, timeout: Duration, ) -> Result<Vec<Source>>

All feeds observed for a hostname, IP or NDI machine name within timeout.

A cache hit returns owned descriptor copies without an SDK call. On a miss, observes the full window using the configured finder; zero makes one non-waiting snapshot attempt. Results keep first-observed order, and sources that differ in name or full address text are distinct. Select the desired feed explicitly. Empty results return an empty vector and are not retained. If the final snapshot lock stays busy until the budget expires, the feeds observed so far are returned uncached and a later miss retries discovery.

Discovery runs outside the cache lock; its window includes snapshot-lock contention. Access to cached descriptor copies can briefly serialize on the cache mutex. Copying and native snapshot execution can finish after the discovery wait budget. Finder::find_sources describes the bounded discovery work and its allocation errors. Copying a cache hit can return Error::AllocationFailed if the result vector cannot be reserved. On a miss, reservations to copy the result, prepare a host key or grow the cache map are best-effort. Creating the shared snapshot owner uses the global allocator’s allocation failure handler, as does creating shared source owners during Finder::current_sources. Entries have the explicit stale-data policy described on SourceCache.

§Errors

Error::InvalidInput for a query outside Source::matches_host’s grammar or a timeout above crate::MAX_TIMEOUT, even on a cache hit. The host is checked before the timeout, including in async lookups; discovery/resource errors propagate without filling an entry. A failed publication reservation returns the discovered result uncached. IP hints must already be configured on the finder.

Source

pub fn invalidate(&self, host: &str)

Remove a host’s complete result set and suppress pending fills. Equivalent spellings remove the same entry. An invalid host removes no entry but still suppresses publication from every pending discovery.

Source

pub fn clear(&self)

Remove every result set and suppress publication from pending discovery.

Source

pub fn len(&self) -> usize

Number of cached host result sets; each holds every feed observed for its host.

Source

pub fn is_empty(&self) -> bool

Whether no host result sets are cached.

Trait Implementations§

Source§

impl Debug for SourceCache

Source§

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

Formats the value using the given formatter. Read more
Source§

impl SourceDiscovery for SourceCache

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