Skip to main content

EngineCache

Struct EngineCache 

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

Thread-safe cache of discovered SNMPv3 engine state.

Before sending authenticated SNMPv3 messages, a client must discover the target engine’s ID, boot counter, and time (RFC 3414 Section 4). This cache stores those results so that subsequent requests, or other clients sharing the same cache via Arc, skip the discovery round trip.

§Entry lifetime

Each entry tracks a synced_at timestamp that is reset on every successful time update (update_time). Entries whose synced_at exceeds the configured TTL (default 5 minutes) are treated as expired: get returns None and the stale entry is removed, causing the next request to re-run discovery.

This TTL-based expiry handles device replacement (a new device with a different engine ID appearing at the same IP address). Without it, the client would hold a stale engine ID indefinitely and every request would fail with usmStatsUnknownEngineIDs. Automatic re-discovery on that Report PDU was considered but rejected because Report PDUs are unauthenticated, making it possible for a spoofed report to force re-discovery toward a rogue engine. The TTL approach avoids this: only entries that have not been refreshed by a successful authenticated exchange are expired.

Actively polled targets refresh their entry on every response, so the TTL has no effect during normal operation.

§Capacity

The cache is unbounded by default. Each entry is roughly 100-150 bytes, so even 100k targets uses only ~10-15 MB. For deployments that scan very large address ranges, with_max_capacity sets a hard limit with oldest-entry eviction.

§Example

use std::sync::Arc;

let cache = Arc::new(EngineCache::new());

let client1 = Client::builder("192.168.1.1:161")
    .username("admin")
    .auth(AuthProtocol::Sha1, "authpass")
    .engine_cache(cache.clone())
    .connect()
    .await?;

let client2 = Client::builder("192.168.1.2:161")
    .username("admin")
    .auth(AuthProtocol::Sha1, "authpass")
    .engine_cache(cache.clone())
    .connect()
    .await?;

Implementations§

Source§

impl EngineCache

Source

pub fn new() -> Self

Create a new empty engine cache with default settings.

Source

pub fn with_max_capacity(self, max_capacity: usize) -> Self

Set a maximum capacity. When full, the oldest entry is evicted on insert.

Source

pub fn with_ttl(self, ttl: Duration) -> Self

Set the TTL for cache entries. Entries not refreshed within this duration are removed on lookup, triggering re-discovery.

Source

pub fn get(&self, target: &SocketAddr) -> Option<EngineState>

Get cached engine state for a target.

Returns None if the entry does not exist or has expired. Expired entries are removed from the cache.

Source

pub fn insert(&self, target: SocketAddr, state: EngineState)

Store engine state for a target.

If a max capacity is set and the cache is full, the entry with the oldest synced_at time is evicted.

Source

pub fn update_time( &self, target: &SocketAddr, response_boots: u32, response_time: u32, ) -> bool

Update time for an existing entry.

Returns true if the entry was updated, false if not found or not updated.

Source

pub fn remove(&self, target: &SocketAddr) -> Option<EngineState>

Remove cached state for a target.

Source

pub fn clear(&self)

Clear all cached state.

Source

pub fn len(&self) -> usize

Get the number of cached engines (including expired entries).

Source

pub fn is_empty(&self) -> bool

Check if the cache is empty.

Trait Implementations§

Source§

impl Debug for EngineCache

Source§

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

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

impl Default for EngineCache

Source§

fn default() -> Self

Returns the “default value” for a type. 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<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
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