Add tasks for cleaning connection and torrent maps

This commit is contained in:
Joakim Frostegård 2020-04-05 03:47:41 +02:00
parent af8b9d05df
commit 268235c8e1
2 changed files with 50 additions and 3 deletions

View file

@ -3,6 +3,7 @@ use std::time::Duration;
mod common;
mod handler;
mod network;
mod tasks;
use common::State;
@ -13,7 +14,7 @@ fn main(){
let socket = network::create_socket(addr, 4096 * 8);
let socket_timeout = Duration::from_millis(1000);
for i in 1..4 {
for i in 0..4 {
let socket = socket.try_clone().unwrap();
let state = state.clone();
@ -22,5 +23,10 @@ fn main(){
});
}
network::run_event_loop(state, socket, 0, socket_timeout);
}
loop {
::std::thread::sleep(Duration::from_secs(30));
tasks::clean_connections(&state);
tasks::clean_torrents(&state);
}
}

41
aquatic/src/tasks.rs Normal file
View file

@ -0,0 +1,41 @@
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use crate::common::*;
pub fn clean_connections(state: &State){
let limit = Instant::now() - Duration::from_secs(300);
state.connections.retain(|_, v| v.0 > limit);
}
pub fn clean_torrents(state: &State){
let limit = Instant::now() - Duration::from_secs(1200);
state.torrents.retain(|_, torrent| {
let num_seeders = &torrent.num_seeders;
let num_leechers = &torrent.num_leechers;
torrent.peers.retain(|_, peer| {
let keep = peer.last_announce.0 > limit;
if !keep {
match peer.status {
PeerStatus::Seeding => {
num_seeders.fetch_sub(1, Ordering::SeqCst);
},
PeerStatus::Leeching => {
num_leechers.fetch_sub(1, Ordering::SeqCst);
},
_ => (),
};
}
keep
});
!torrent.peers.is_empty()
});
}