diff --git a/aquatic_udp/src/lib/common.rs b/aquatic_udp/src/lib/common.rs index 41381ed..2a87296 100644 --- a/aquatic_udp/src/lib/common.rs +++ b/aquatic_udp/src/lib/common.rs @@ -234,6 +234,8 @@ pub struct Statistics { pub bytes_sent: AtomicUsize, pub torrents_ipv4: Vec, pub torrents_ipv6: Vec, + pub peers_ipv4: Vec, + pub peers_ipv6: Vec, } impl Statistics { @@ -245,6 +247,8 @@ impl Statistics { bytes_sent: Default::default(), torrents_ipv4: Self::create_atomic_usize_vec(num_request_workers), torrents_ipv6: Self::create_atomic_usize_vec(num_request_workers), + peers_ipv4: Self::create_atomic_usize_vec(num_request_workers), + peers_ipv6: Self::create_atomic_usize_vec(num_request_workers), } } diff --git a/aquatic_udp/src/lib/handlers.rs b/aquatic_udp/src/lib/handlers.rs index 5cc38f0..f333f2f 100644 --- a/aquatic_udp/src/lib/handlers.rs +++ b/aquatic_udp/src/lib/handlers.rs @@ -128,6 +128,14 @@ pub fn run_request_worker( if now > last_cleaning + cleaning_interval { torrents.clean(&config, &state.access_list); + if !statistics_update_interval.is_zero() { + let peers_ipv4 = torrents.ipv4.values().map(|t| t.peers.len()).sum(); + let peers_ipv6 = torrents.ipv6.values().map(|t| t.peers.len()).sum(); + + state.statistics.peers_ipv4[worker_index.0].store(peers_ipv4, Ordering::SeqCst); + state.statistics.peers_ipv6[worker_index.0].store(peers_ipv6, Ordering::SeqCst); + } + last_cleaning = now; } if !statistics_update_interval.is_zero() diff --git a/aquatic_udp/src/lib/tasks.rs b/aquatic_udp/src/lib/tasks.rs index b175baa..5ca61ce 100644 --- a/aquatic_udp/src/lib/tasks.rs +++ b/aquatic_udp/src/lib/tasks.rs @@ -1,4 +1,4 @@ -use std::sync::atomic::Ordering; +use std::sync::atomic::{AtomicUsize, Ordering}; use super::common::*; use crate::config::Config; @@ -37,6 +37,8 @@ pub fn gather_and_print_statistics(state: &State, config: &Config) { .iter() .map(|n| n.load(Ordering::SeqCst)) .sum(); + let num_peers_ipv4 = sum_atomic_usize_vec(&state.statistics.peers_ipv4); + let num_peers_ipv6 = sum_atomic_usize_vec(&state.statistics.peers_ipv6); let access_list_len = state.access_list.load().len(); @@ -55,8 +57,16 @@ pub fn gather_and_print_statistics(state: &State, config: &Config) { "ipv4 torrents: {}, ipv6 torrents: {}", num_torrents_ipv4, num_torrents_ipv6, ); + println!( + "ipv4 peers: {}, ipv6 peers: {} (both updated every {} seconds)", + num_peers_ipv4, num_peers_ipv6, config.cleaning.torrent_cleaning_interval + ); println!("access list entries: {}", access_list_len,); println!(); } + +fn sum_atomic_usize_vec(vec: &Vec) -> usize { + vec.iter().map(|n| n.load(Ordering::SeqCst)).sum() +}