diff --git a/aquatic/src/main.rs b/aquatic/src/main.rs index 301a118..d7915d6 100644 --- a/aquatic/src/main.rs +++ b/aquatic/src/main.rs @@ -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); + } +} \ No newline at end of file diff --git a/aquatic/src/tasks.rs b/aquatic/src/tasks.rs new file mode 100644 index 0000000..403311a --- /dev/null +++ b/aquatic/src/tasks.rs @@ -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() + }); +} \ No newline at end of file