aquatic_ws: almost finish implementing access list support

This commit is contained in:
Joakim Frostegård 2021-10-16 01:08:59 +02:00
parent db596b5038
commit 28cc6c261a
5 changed files with 88 additions and 36 deletions

View file

@ -1,45 +1,23 @@
use std::time::Instant;
use aquatic_common::access_list::AccessListMode;
use histogram::Histogram;
use crate::common::*;
use crate::config::Config;
pub fn clean_torrents(state: &State) {
fn clean_torrent_map(torrent_map: &mut TorrentMap) {
let now = Instant::now();
torrent_map.retain(|_, torrent_data| {
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();
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)
{
::log::error!("Couldn't update access list: {:?}", err);
}
}
AccessListMode::Ignore => {}
}
let mut torrent_maps = state.torrent_maps.lock();
clean_torrent_map(&mut torrent_maps.ipv4);
clean_torrent_map(&mut torrent_maps.ipv6);
}
pub fn print_statistics(state: &State) {