aquatic: add CleaningConfig and StatisticsConfig to Config, use them

This commit is contained in:
Joakim Frostegård 2020-04-08 21:22:12 +02:00
parent 766606cc08
commit fa60eddcc4
3 changed files with 54 additions and 11 deletions

View file

@ -9,7 +9,23 @@ pub struct Config {
pub poll_event_capacity: usize, pub poll_event_capacity: usize,
pub max_scrape_torrents: u8, pub max_scrape_torrents: u8,
pub max_response_peers: usize, pub max_response_peers: usize,
pub statistics_interval: u64, pub statistics: StatisticsConfig,
pub cleaning: CleaningConfig,
}
#[derive(Clone)]
pub struct StatisticsConfig {
pub interval: u64,
}
#[derive(Clone)]
pub struct CleaningConfig {
pub interval: u64,
pub max_peer_age: u64,
pub max_connection_age: u64,
} }
@ -22,7 +38,28 @@ impl Default for Config {
recv_buffer_size: 4096 * 128, recv_buffer_size: 4096 * 128,
max_scrape_torrents: 255, max_scrape_torrents: 255,
max_response_peers: 255, max_response_peers: 255,
statistics_interval: 5, statistics: StatisticsConfig::default(),
cleaning: CleaningConfig::default(),
}
}
}
impl Default for StatisticsConfig {
fn default() -> Self {
Self {
interval: 5,
}
}
}
impl Default for CleaningConfig {
fn default() -> Self {
Self {
interval: 30,
max_peer_age: 60 * 20,
max_connection_age: 60 * 5,
} }
} }
} }

View file

@ -26,11 +26,12 @@ pub fn run(){
}); });
} }
{ if config.statistics.interval != 0 {
let state = state.clone(); let state = state.clone();
let config = config.clone();
::std::thread::spawn(move || { ::std::thread::spawn(move || {
let interval = config.statistics_interval; let interval = config.statistics.interval;
loop { loop {
::std::thread::sleep(Duration::from_secs(interval)); ::std::thread::sleep(Duration::from_secs(interval));
@ -102,9 +103,9 @@ pub fn run(){
} }
loop { loop {
::std::thread::sleep(Duration::from_secs(30)); ::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
tasks::clean_connections(&state); tasks::clean_connections(&state, &config);
tasks::clean_torrents(&state); tasks::clean_torrents(&state, &config);
} }
} }

View file

@ -2,18 +2,23 @@ use std::sync::atomic::Ordering;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use crate::common::*; use crate::common::*;
use crate::config::Config;
pub fn clean_connections(state: &State){ pub fn clean_connections(state: &State, config: &Config){
let limit = Instant::now() - Duration::from_secs(300); let limit = Instant::now() - Duration::from_secs(
config.cleaning.max_connection_age
);
state.connections.retain(|_, v| v.0 > limit); state.connections.retain(|_, v| v.0 > limit);
state.connections.shrink_to_fit(); state.connections.shrink_to_fit();
} }
pub fn clean_torrents(state: &State){ pub fn clean_torrents(state: &State, config: &Config){
let limit = Instant::now() - Duration::from_secs(1200); let limit = Instant::now() - Duration::from_secs(
config.cleaning.max_peer_age
);
state.torrents.retain(|_, torrent| { state.torrents.retain(|_, torrent| {
let num_seeders = &torrent.num_seeders; let num_seeders = &torrent.num_seeders;