Reduce ValidUntil size; reduce size of various ws structs

This commit is contained in:
Joakim Frostegård 2022-08-01 14:15:06 +02:00
parent 97fa699476
commit fcf18c845f
21 changed files with 343 additions and 193 deletions

View file

@ -1,7 +1,6 @@
use std::net::{Ipv4Addr, Ipv6Addr};
use std::time::Instant;
use aquatic_common::{AmortizedIndexMap, ValidUntil};
use aquatic_common::{AmortizedIndexMap, SecondsSinceServerStart, ServerStartInstant, ValidUntil};
use aquatic_http_protocol::common::{AnnounceEvent, InfoHash, PeerId};
use aquatic_http_protocol::response::ResponsePeer;
@ -84,20 +83,20 @@ pub struct TorrentMaps {
}
impl TorrentMaps {
pub fn clean(&mut self) {
Self::clean_torrent_map(&mut self.ipv4);
Self::clean_torrent_map(&mut self.ipv6);
pub fn clean(&mut self, server_start_instant: ServerStartInstant) {
let now = server_start_instant.seconds_elapsed();
Self::clean_torrent_map(&mut self.ipv4, now);
Self::clean_torrent_map(&mut self.ipv6, now);
}
fn clean_torrent_map<I: Ip>(torrent_map: &mut TorrentMap<I>) {
let now = Instant::now();
fn clean_torrent_map<I: Ip>(torrent_map: &mut TorrentMap<I>, now: SecondsSinceServerStart) {
torrent_map.retain(|_, torrent_data| {
let num_seeders = &mut torrent_data.num_seeders;
let num_leechers = &mut torrent_data.num_leechers;
torrent_data.peers.retain(|_, peer| {
if peer.valid_until.0 >= now {
if peer.valid_until.valid(now) {
true
} else {
match peer.status {

View file

@ -11,7 +11,9 @@ use tokio::sync::mpsc::Receiver;
use tokio::task::LocalSet;
use tokio::time;
use aquatic_common::{extract_response_peers, CanonicalSocketAddr, PanicSentinel, ValidUntil};
use aquatic_common::{
extract_response_peers, CanonicalSocketAddr, PanicSentinel, ServerStartInstant, ValidUntil,
};
use aquatic_http_protocol::response::{
AnnounceResponse, Response, ResponsePeer, ResponsePeerListV4, ResponsePeerListV6,
};
@ -25,12 +27,13 @@ pub fn run_swarm_worker(
_sentinel: PanicSentinel,
config: Config,
request_receiver: Receiver<ChannelAnnounceRequest>,
server_start_instant: ServerStartInstant,
) -> anyhow::Result<()> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
runtime.block_on(run_inner(config, request_receiver))?;
runtime.block_on(run_inner(config, request_receiver, server_start_instant))?;
Ok(())
}
@ -38,6 +41,7 @@ pub fn run_swarm_worker(
async fn run_inner(
config: Config,
mut request_receiver: Receiver<ChannelAnnounceRequest>,
server_start_instant: ServerStartInstant,
) -> anyhow::Result<()> {
let torrents = Rc::new(RefCell::new(TorrentMaps::default()));
let mut rng = SmallRng::from_entropy();
@ -45,6 +49,7 @@ async fn run_inner(
LocalSet::new().spawn_local(periodically_clean_torrents(
config.clone(),
torrents.clone(),
server_start_instant,
));
loop {
@ -53,7 +58,7 @@ async fn run_inner(
.await
.ok_or_else(|| anyhow::anyhow!("request channel closed"))?;
let valid_until = ValidUntil::new(config.cleaning.max_peer_age);
let valid_until = ValidUntil::new(server_start_instant, config.cleaning.max_peer_age);
let response = handle_announce_request(
&config,
@ -68,7 +73,11 @@ async fn run_inner(
}
}
async fn periodically_clean_torrents(config: Config, torrents: Rc<RefCell<TorrentMaps>>) {
async fn periodically_clean_torrents(
config: Config,
torrents: Rc<RefCell<TorrentMaps>>,
server_start_instant: ServerStartInstant,
) {
let mut interval = time::interval(time::Duration::from_secs(
config.cleaning.torrent_cleaning_interval,
));
@ -76,7 +85,7 @@ async fn periodically_clean_torrents(config: Config, torrents: Rc<RefCell<Torren
loop {
interval.tick().await;
torrents.borrow_mut().clean();
torrents.borrow_mut().clean(server_start_instant);
}
}