implement index pool auto clean on limits overflow

This commit is contained in:
yggverse 2025-06-28 20:12:57 +03:00
parent 56830be4a9
commit 9643adbe99
4 changed files with 69 additions and 19 deletions

View file

@ -7,22 +7,69 @@ use std::{
sync::RwLock,
};
/// Collect peer requests to print stats and visitors count
pub struct Request(Option<RwLock<HashMap<IpAddr, Vec<Query>>>>);
/// Collect peer requests for stats and visitors count
pub struct Request {
index: Option<RwLock<HashMap<IpAddr, Vec<Query>>>>,
// prevent log file overflow by recording error events once
is_max_peers_reported: RwLock<bool>,
is_max_peer_queries_reported: RwLock<bool>,
}
impl Request {
pub fn init(is_enabled: bool) -> Self {
if is_enabled {
Self(Some(RwLock::new(HashMap::with_capacity(100))))
} else {
Self(None)
Self {
index: if is_enabled {
Some(RwLock::new(HashMap::with_capacity(100)))
} else {
None
},
is_max_peers_reported: RwLock::new(false),
is_max_peer_queries_reported: RwLock::new(false),
}
}
pub fn add(&self, peer: &SocketAddr, query: &str) {
if let Some(ref this) = self.0 {
this.write()
.unwrap()
if let Some(ref this) = self.index {
let mut index = this.write().unwrap();
// Critical limits to forcefully free one memory slot(s) for the new record
// * the query len is already limited by the read buffer (1024 bytes * LIMIT)
// * make it optional @TODO
const INDEX_MAX_PEERS: usize = 1000;
const INDEX_MAX_PEER_QUERIES: usize = 1000;
if index.len() >= INDEX_MAX_PEERS {
let mut r = self.is_max_peers_reported.write().unwrap();
if !*r {
*r = true;
eprintln!("Max peers index limit ({INDEX_MAX_PEERS}) reached");
}
let k = *index.keys().next().unwrap();
index.remove(&k); // * there is no difference which one key to free for the HashMap
}
for queries in index.values_mut() {
if queries.len() >= INDEX_MAX_PEER_QUERIES {
let mut r = self.is_max_peer_queries_reported.write().unwrap();
if !*r {
*r = true;
eprintln!(
"Max queries limit ({INDEX_MAX_PEER_QUERIES}) reached for `{peer}`"
);
}
queries.truncate(INDEX_MAX_PEER_QUERIES - 1) // free last slot for one query
}
}
// Cleanup deprecated records
// * this is expensive for each request, use schedule instead @TODO
let d = chrono::Local::now().date_naive();
for queries in index.values_mut() {
queries.retain(|q| q.time.date_naive() == d)
}
index.retain(|_, q| !q.is_empty());
// handle new record
index
.entry(peer.ip())
.and_modify(|c| c.push(Query::new(query)))
.or_insert(vec![Query::new(query)]);
@ -30,8 +77,8 @@ impl Request {
}
pub fn count(&self) -> usize {
if let Some(ref this) = self.0 {
this.read().unwrap().len()
if let Some(ref i) = self.index {
i.read().unwrap().len()
} else {
0
}
@ -39,8 +86,8 @@ impl Request {
pub fn total(&self, query_prefix: Option<&str>) -> usize {
let mut t = 0;
if let Some(ref this) = self.0 {
for queries in this.read().unwrap().values() {
if let Some(ref i) = self.index {
for queries in i.read().unwrap().values() {
match query_prefix {
Some(p) => {
for q in queries {