pub struct EngineCache { /* private fields */ }Expand description
Thread-safe cache of discovered SNMPv3 engine state.
Target addresses map to discovered identities and remote message-size limits. Trusted time is keyed separately by authoritative engine ID, so clients reaching the same engine through multiple targets converge on one high-water value. Whole-state inserts merge monotonically and cannot replace a newer trusted tuple with a stale clone.
§Entry lifetime
Each target identity has a refresh timestamp. Every accepted HMAC-verified
message refreshes it, including an older in-window message that does not
advance trusted time. Entries older than the configured TTL
(default 5 minutes) are removed by get.
Expiry prevents a shared entry from being handed indefinitely to newly
constructed clients after a target is replaced. It does not silently clear
an existing client’s established identity; call
Client::rediscover_engine to replace it
intentionally.
Actively polled authenticated targets refresh their entry on every accepted HMAC-verified response or Report, so the TTL has no effect during normal authenticated 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 async_snmp::{Auth, AuthProtocol, Client, EngineCache};
use std::sync::Arc;
let cache = Arc::new(EngineCache::new());
let client1 = Client::builder("192.168.1.1:161",
Auth::usm("admin").auth(AuthProtocol::Sha1, "authpass"))
.engine_cache(cache.clone())
.connect()
.await?;
let client2 = Client::builder("192.168.1.2:161",
Auth::usm("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 least recently refreshed target identity 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 after authenticating a message.
The caller must have verified the message HMAC and engine identity. 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 identity for a target. Shared trusted time remains while another target still maps to the same authoritative engine.
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