mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
Add tasks for cleaning connection and torrent maps
This commit is contained in:
parent
af8b9d05df
commit
268235c8e1
2 changed files with 50 additions and 3 deletions
|
|
@ -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
41
aquatic/src/tasks.rs
Normal 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()
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue