use max items time offset instead of totals count formula

This commit is contained in:
yggverse 2025-08-14 01:56:08 +03:00
parent d1d628e878
commit a087a236b7

View file

@ -9,14 +9,14 @@ pub struct Item {
pub struct Ban { pub struct Ban {
index: HashMap<Id20, DateTime<Local>>, index: HashMap<Id20, DateTime<Local>>,
timeout: u64, timeout: Duration,
} }
impl Ban { impl Ban {
pub fn init(timeout: u64, capacity: usize) -> Self { pub fn init(timeout: u64, capacity: usize) -> Self {
Self { Self {
index: HashMap::with_capacity(capacity), index: HashMap::with_capacity(capacity),
timeout, timeout: Duration::from_secs(timeout),
} }
} }
@ -28,7 +28,7 @@ impl Ban {
self.index.len() self.index.len()
} }
/// * returns removed `Item` details /// * return removed `Item` details
pub fn update(&mut self, time: DateTime<Local>) -> Vec<Item> { pub fn update(&mut self, time: DateTime<Local>) -> Vec<Item> {
let mut b = Vec::with_capacity(self.index.len()); let mut b = Vec::with_capacity(self.index.len());
self.index.retain(|i, &mut expires| { self.index.retain(|i, &mut expires| {
@ -45,9 +45,9 @@ impl Ban {
b b
} }
/// * returns expiration time /// * return expiration time
pub fn add(&mut self, key: Id20) -> DateTime<Local> { pub fn add(&mut self, key: Id20) -> DateTime<Local> {
let t = Local::now() + Duration::from_secs(self.index.len() as u64 * self.timeout); let t = self.index.values().max().map_or(Local::now(), |t| *t) + self.timeout;
assert!(self.index.insert(key, t).is_none()); assert!(self.index.insert(key, t).is_none());
t t
} }