add permanent ban support (to handle unresolvable torrent errors / todo)

This commit is contained in:
yggverse 2025-08-18 17:38:49 +03:00
parent 0332ec0788
commit e77b3bb5ad
2 changed files with 45 additions and 18 deletions

View file

@ -8,7 +8,7 @@ pub struct Item {
}
pub struct Ban {
index: HashMap<Id20, DateTime<Local>>,
index: HashMap<Id20, Option<DateTime<Local>>>,
timeout: Duration,
}
@ -20,7 +20,7 @@ impl Ban {
}
}
pub fn get(&self, key: &Id20) -> Option<&DateTime<Local>> {
pub fn get(&self, key: &Id20) -> Option<&Option<DateTime<Local>>> {
self.index.get(key)
}
@ -31,8 +31,10 @@ impl Ban {
/// * return removed `Item` details
pub fn update(&mut self, time: DateTime<Local>) -> Vec<Item> {
let mut b = Vec::with_capacity(self.index.len());
self.index.retain(|i, &mut expires| {
if time > expires {
self.index.retain(|i, &mut e| {
if let Some(expires) = e
&& time > expires
{
b.push(Item {
expires,
info_hash: i.as_string(),
@ -45,13 +47,23 @@ impl Ban {
b
}
/// * return expiration time
pub fn add(&mut self, key: Id20) -> DateTime<Local> {
let t = self
.index
.values()
.max()
.map_or(Local::now(), |t| *t + self.timeout);
/// Add torrent to the ban list
///
/// If the `is_permanent` option is `true` - ban permanently,
/// or **automatically** count optimal ban time based on the current index max time and timeout offset value.
///
/// * return expiration time or `None` if the ban is permanent
pub fn add(&mut self, key: Id20, is_permanent: bool) -> Option<DateTime<Local>> {
let t = if is_permanent {
None
} else {
Some(
self.index
.values()
.max()
.map_or(Local::now(), |t| t.unwrap_or(Local::now()) + self.timeout),
)
};
assert!(self.index.insert(key, t).is_none());
t
}