mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_http: move torrent cleaning code to TorrentMaps impl
This commit is contained in:
parent
10fe014c03
commit
ddb1f394a1
3 changed files with 63 additions and 65 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use aquatic_common::access_list::AccessList;
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
|
|
@ -17,6 +18,8 @@ use aquatic_http_protocol::common::*;
|
|||
use aquatic_http_protocol::request::Request;
|
||||
use aquatic_http_protocol::response::{Response, ResponsePeer};
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
pub const LISTENER_TOKEN: Token = Token(0);
|
||||
pub const CHANNEL_TOKEN: Token = Token(1);
|
||||
|
||||
|
|
@ -115,6 +118,52 @@ pub struct TorrentMaps {
|
|||
pub access_list: AccessList,
|
||||
}
|
||||
|
||||
impl TorrentMaps {
|
||||
pub fn clean(&mut self, config: &Config) {
|
||||
Self::clean_torrent_map(config, &self.access_list, &mut self.ipv4);
|
||||
Self::clean_torrent_map(config, &self.access_list, &mut self.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();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub torrent_maps: Arc<Mutex<TorrentMaps>>,
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ use mio::{Poll, Waker};
|
|||
use parking_lot::Mutex;
|
||||
use privdrop::PrivDrop;
|
||||
|
||||
use aquatic_common::access_list::AccessListMode;
|
||||
|
||||
pub mod common;
|
||||
pub mod config;
|
||||
pub mod handler;
|
||||
|
|
@ -24,23 +22,18 @@ pub const APP_NAME: &str = "aquatic_http: HTTP/TLS BitTorrent tracker";
|
|||
pub fn run(config: Config) -> anyhow::Result<()> {
|
||||
let state = State::default();
|
||||
|
||||
match config.access_list.mode {
|
||||
AccessListMode::Require | AccessListMode::Forbid => {
|
||||
state
|
||||
.torrent_maps
|
||||
.lock()
|
||||
.access_list
|
||||
.update_from_path(&config.access_list.path)?;
|
||||
}
|
||||
AccessListMode::Ignore => {}
|
||||
}
|
||||
tasks::update_access_list(&config, &mut state.torrent_maps.lock());
|
||||
|
||||
start_workers(config.clone(), state.clone())?;
|
||||
|
||||
loop {
|
||||
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
||||
|
||||
tasks::clean_torrents(&config, &state);
|
||||
let mut torrent_maps = state.torrent_maps.lock();
|
||||
|
||||
tasks::update_access_list(&config, &mut torrent_maps);
|
||||
|
||||
torrent_maps.clean(&config);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue