aquatic_http: move torrent cleaning code to TorrentMaps impl

This commit is contained in:
Joakim Frostegård 2021-10-15 23:13:26 +02:00
parent 10fe014c03
commit ddb1f394a1
3 changed files with 63 additions and 65 deletions

View file

@ -1,65 +1,21 @@
use std::{ops::DerefMut, time::Instant};
use histogram::Histogram;
use aquatic_common::access_list::{AccessList, AccessListMode};
use aquatic_common::access_list::AccessListMode;
use crate::{common::*, config::Config};
pub fn clean_torrents(config: &Config, state: &State) {
let mut torrent_maps = state.torrent_maps.lock();
let torrent_maps = torrent_maps.deref_mut();
pub fn update_access_list(config: &Config, torrent_maps: &mut TorrentMaps) {
match config.access_list.mode {
AccessListMode::Require | AccessListMode::Forbid => {
if let Err(err) = torrent_maps.access_list.update_from_path(&config.access_list.path) {
if let Err(err) = torrent_maps
.access_list
.update_from_path(&config.access_list.path)
{
::log::error!("Couldn't update access list: {:?}", err);
}
},
AccessListMode::Ignore => { }
}
clean_torrent_map(config, &torrent_maps.access_list, &mut torrent_maps.ipv4);
clean_torrent_map(config, &torrent_maps.access_list, &mut torrent_maps.ipv6);
}
fn clean_torrent_map<I: Ip>(
config: &Config,
access_list: &AccessList,
torrent_map: &mut TorrentMap<I>,
) {
let now = Instant::now();
torrent_map.retain(|info_hash, torrent_data| {
if !access_list.allows(config.access_list.mode, &info_hash.0) {
return false;
}
let num_seeders = &mut torrent_data.num_seeders;
let num_leechers = &mut torrent_data.num_leechers;
torrent_data.peers.retain(|_, peer| {
let keep = peer.valid_until.0 >= now;
if !keep {
match peer.status {
PeerStatus::Seeding => {
*num_seeders -= 1;
}
PeerStatus::Leeching => {
*num_leechers -= 1;
}
_ => (),
};
}
keep
});
!torrent_data.peers.is_empty()
});
torrent_map.shrink_to_fit();
AccessListMode::Ignore => {}
}
}
pub fn print_statistics(state: &State) {