makefile and service
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use lru::LruCache;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::num::NonZeroUsize;
|
||||
use tracing::{debug, info};
|
||||
|
||||
pub struct FakeIpStore {
|
||||
cache: LruCache<String, Ipv4Addr>,
|
||||
@@ -12,6 +13,7 @@ const START_IP: u32 = 0x64400001; // 100.64.0.1 (IP for Service Networks/Shared
|
||||
|
||||
impl FakeIpStore {
|
||||
pub fn new() -> Self {
|
||||
info!("Initializing FakeIpStore starting at 100.64.0.1");
|
||||
Self {
|
||||
cache: LruCache::new(NonZeroUsize::new(2000).unwrap()),
|
||||
rev_cache: LruCache::new(NonZeroUsize::new(2000).unwrap()),
|
||||
@@ -21,16 +23,24 @@ impl FakeIpStore {
|
||||
|
||||
pub fn get_or_assign(&mut self, host: &str) -> Ipv4Addr {
|
||||
if let Some(&ip) = self.cache.get(host) {
|
||||
debug!(host = %host, ip = %ip, "Cache hit: IP already assigned");
|
||||
return ip;
|
||||
}
|
||||
let ip = Ipv4Addr::from(self.next_ip);
|
||||
self.next_ip += 1;
|
||||
self.cache.put(host.to_string(), ip);
|
||||
self.rev_cache.put(ip, host.to_string());
|
||||
info!(host = %host, ip = %ip, "Assigned new fake IP");
|
||||
ip
|
||||
}
|
||||
|
||||
pub fn lookup_by_ip(&self, ip: &Ipv4Addr) -> Option<String> {
|
||||
self.rev_cache.peek(ip).cloned()
|
||||
if let Some(host) = self.rev_cache.peek(ip) {
|
||||
debug!(ip = %ip, host = %host, "Reverse lookup successful");
|
||||
Some(host.clone())
|
||||
} else {
|
||||
debug!(ip = %ip, "Reverse lookup miss");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user