use local timestamp for the ban timeout value

This commit is contained in:
yggverse 2025-08-13 16:04:16 +03:00
parent 93df9e6df8
commit 22199c55e6

View file

@ -3,7 +3,7 @@ mod config;
mod preload; mod preload;
use anyhow::Result; use anyhow::Result;
use chrono::{DateTime, Utc}; use chrono::DateTime;
use config::Config; use config::Config;
use librqbit::{ use librqbit::{
AddTorrent, AddTorrentOptions, AddTorrentResponse, ConnectionOptions, PeerConnectionOptions, AddTorrent, AddTorrentOptions, AddTorrentResponse, ConnectionOptions, PeerConnectionOptions,
@ -64,14 +64,14 @@ async fn main() -> Result<()> {
}, },
) )
.await?; .await?;
let mut ban: HashMap<librqbit::dht::Id20, DateTime<Utc>> = let mut ban: HashMap<librqbit::dht::Id20, DateTime<Local>> =
HashMap::with_capacity(config.index_capacity); HashMap::with_capacity(config.index_capacity);
log::info!("crawler started at {time_init}"); log::info!("crawler started at {time_init}");
loop { loop {
let time_queue = Local::now(); let time_queue = Local::now();
log::debug!("queue crawl begin at {time_queue}..."); log::debug!("queue crawl begin at {time_queue}...");
ban.retain(|k, &mut v| { ban.retain(|k, &mut v| {
let is_expired = v > Utc::now() - Duration::from_secs(config.ban); let is_expired = v > time_queue - Duration::from_secs(config.ban);
if is_expired { if is_expired {
log::debug!( log::debug!(
"remove `{}` from the ban list by timeout expire ({v})", "remove `{}` from the ban list by timeout expire ({v})",
@ -201,7 +201,7 @@ async fn main() -> Result<()> {
log::debug!( log::debug!(
"skip awaiting the completion of preload `{h}` data (`{e}`)" "skip awaiting the completion of preload `{h}` data (`{e}`)"
); );
ban.insert(i, Utc::now()); ban.insert(i, Local::now());
session session
.delete(librqbit::api::TorrentIdOrHash::Id(id), false) .delete(librqbit::api::TorrentIdOrHash::Id(id), false)
.await?; // * do not collect billions of slow torrents in the session pool .await?; // * do not collect billions of slow torrents in the session pool
@ -223,14 +223,14 @@ async fn main() -> Result<()> {
Ok(_) => panic!(), Ok(_) => panic!(),
Err(e) => { Err(e) => {
log::debug!("failed to resolve torrent `{h}`: `{e}`."); log::debug!("failed to resolve torrent `{h}`: `{e}`.");
assert!(ban.insert(i, Utc::now()).is_none()); assert!(ban.insert(i, Local::now()).is_none());
} }
}, },
Err(e) => { Err(e) => {
log::debug!( log::debug!(
"skip awaiting the completion of adding torrent `{h}` data (`{e}`)" "skip awaiting the completion of adding torrent `{h}` data (`{e}`)"
); );
assert!(ban.insert(i, Utc::now()).is_none()); assert!(ban.insert(i, Local::now()).is_none());
} }
} }
} }