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
impl EngineCache
Sourcepub fn with_max_capacity(self, max_capacity: usize) -> Self
pub fn with_max_capacity(self, max_capacity: usize) -> Self
Set a maximum capacity. When full, the oldest entry is evicted on insert.
Sourcepub fn with_ttl(self, ttl: Duration) -> Self
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.
Sourcepub fn get(&self, target: &SocketAddr) -> Option<EngineState>
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.
Sourcepub fn insert(&self, target: SocketAddr, state: EngineState)
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.
Sourcepub fn update_time(
&self,
target: &SocketAddr,
response_boots: u32,
response_time: u32,
) -> bool
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.
Sourcepub fn remove(&self, target: &SocketAddr) -> Option<EngineState>
pub fn remove(&self, target: &SocketAddr) -> Option<EngineState>
Remove cached state for a target.
Trait Implementations§
Source§impl Debug for EngineCache
impl Debug for EngineCache
Auto Trait Implementations§
impl !Freeze for EngineCache
impl RefUnwindSafe for EngineCache
impl Send for EngineCache
impl Sync for EngineCache
impl Unpin for EngineCache
impl UnsafeUnpin for EngineCache
impl UnwindSafe for EngineCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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