From 31087f7936a89329cf0269f6424910f540469a48 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 13 Aug 2025 19:45:38 +0300 Subject: [PATCH] implement ban Item, handle update results outside --- src/ban.rs | 20 ++++++++++++++------ src/main.rs | 8 +++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ban.rs b/src/ban.rs index 937e471..9ce904f 100644 --- a/src/ban.rs +++ b/src/ban.rs @@ -2,6 +2,11 @@ use chrono::{DateTime, Local}; use librqbit::dht::Id20; use std::{collections::HashMap, time::Duration}; +pub struct Item { + pub expires: DateTime, + pub info_hash: String, +} + pub struct Ban { index: HashMap>, timeout: u64, @@ -23,20 +28,23 @@ impl Ban { self.index.len() } - pub fn update(&mut self, time: DateTime) { + pub fn update(&mut self, time: DateTime) -> Vec { + let mut b = Vec::with_capacity(self.index.len()); self.index.retain(|i, &mut expires| { if time > expires { - log::debug!( - "remove ban for `{}` by the timeout expiration ({expires})", - i.as_string() - ); + b.push(Item { + expires, + info_hash: i.as_string(), + }); false } else { true } - }) + }); + b } + /// * returns expiration time pub fn add(&mut self, key: Id20) -> DateTime { let t = Local::now() + Duration::from_secs(self.index.len() as u64 * self.timeout); assert!(self.index.insert(key, t).is_none()); diff --git a/src/main.rs b/src/main.rs index cc9d0da..7063aee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,13 @@ async fn main() -> Result<()> { loop { let time_queue = Local::now(); log::debug!("queue crawl begin at {time_queue}..."); - ban.update(time_queue); + for r in ban.update(time_queue) { + log::debug!( + "remove the ban for `{}` as it has expired on {}", + r.info_hash, + r.expires + ) + } for source in &config.infohash { log::debug!("index source `{source}`..."); // grab latest info-hashes from this source