mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
Reduce ValidUntil size; reduce size of various ws structs
This commit is contained in:
parent
97fa699476
commit
fcf18c845f
21 changed files with 343 additions and 193 deletions
|
|
@ -181,10 +181,10 @@ pub struct CleaningConfig {
|
|||
/// Allow clients to use a connection token for this long (seconds)
|
||||
pub max_connection_age: u32,
|
||||
/// Remove peers who have not announced for this long (seconds)
|
||||
pub max_peer_age: u64,
|
||||
pub max_peer_age: u32,
|
||||
/// Remove pending scrape responses that have not been returned from swarm
|
||||
/// workers for this long (seconds)
|
||||
pub max_pending_scrape_age: u64,
|
||||
pub max_pending_scrape_age: u32,
|
||||
}
|
||||
|
||||
impl Default for CleaningConfig {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use aquatic_common::access_list::update_access_list;
|
|||
#[cfg(feature = "cpu-pinning")]
|
||||
use aquatic_common::cpu_pinning::{pin_current_if_configured_to, WorkerIndex};
|
||||
use aquatic_common::privileges::PrivilegeDropper;
|
||||
use aquatic_common::PanicSentinelWatcher;
|
||||
use aquatic_common::{PanicSentinelWatcher, ServerStartInstant};
|
||||
|
||||
use common::{
|
||||
ConnectedRequestSender, ConnectedResponseSender, SocketWorkerIndex, State, SwarmWorkerIndex,
|
||||
|
|
@ -41,6 +41,8 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
|||
let mut response_senders = Vec::new();
|
||||
let mut response_receivers = BTreeMap::new();
|
||||
|
||||
let server_start_instant = ServerStartInstant::new();
|
||||
|
||||
for i in 0..config.swarm_workers {
|
||||
let (request_sender, request_receiver) = if config.worker_channel_size == 0 {
|
||||
unbounded()
|
||||
|
|
@ -85,6 +87,7 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
|||
sentinel,
|
||||
config,
|
||||
state,
|
||||
server_start_instant,
|
||||
request_receiver,
|
||||
response_sender,
|
||||
SwarmWorkerIndex(i),
|
||||
|
|
@ -120,6 +123,7 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
|||
config,
|
||||
i,
|
||||
connection_validator,
|
||||
server_start_instant,
|
||||
request_sender,
|
||||
response_receiver,
|
||||
priv_dropper,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ pub mod validator;
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use anyhow::Context;
|
||||
use aquatic_common::ServerStartInstant;
|
||||
use crossbeam_channel::Receiver;
|
||||
use mio::net::UdpSocket;
|
||||
use mio::{Events, Interest, Poll, Token};
|
||||
|
|
@ -31,6 +32,7 @@ pub fn run_socket_worker(
|
|||
config: Config,
|
||||
token_num: usize,
|
||||
mut connection_validator: ConnectionValidator,
|
||||
server_start_instant: ServerStartInstant,
|
||||
request_sender: ConnectedRequestSender,
|
||||
response_receiver: Receiver<(ConnectedResponse, CanonicalSocketAddr)>,
|
||||
priv_dropper: PrivilegeDropper,
|
||||
|
|
@ -59,7 +61,8 @@ pub fn run_socket_worker(
|
|||
let pending_scrape_cleaning_duration =
|
||||
Duration::from_secs(config.cleaning.pending_scrape_cleaning_interval);
|
||||
|
||||
let mut pending_scrape_valid_until = ValidUntil::new(config.cleaning.max_pending_scrape_age);
|
||||
let mut pending_scrape_valid_until =
|
||||
ValidUntil::new(server_start_instant, config.cleaning.max_pending_scrape_age);
|
||||
let mut last_pending_scrape_cleaning = Instant::now();
|
||||
|
||||
let mut iter_counter = 0usize;
|
||||
|
|
@ -100,13 +103,17 @@ pub fn run_socket_worker(
|
|||
|
||||
// Run periodic ValidUntil updates and state cleaning
|
||||
if iter_counter % 256 == 0 {
|
||||
let seconds_since_start = server_start_instant.seconds_elapsed();
|
||||
|
||||
pending_scrape_valid_until = ValidUntil::new_with_now(
|
||||
seconds_since_start,
|
||||
config.cleaning.max_pending_scrape_age,
|
||||
);
|
||||
|
||||
let now = Instant::now();
|
||||
|
||||
pending_scrape_valid_until =
|
||||
ValidUntil::new_with_now(now, config.cleaning.max_pending_scrape_age);
|
||||
|
||||
if now > last_pending_scrape_cleaning + pending_scrape_cleaning_duration {
|
||||
pending_scrape_responses.clean();
|
||||
pending_scrape_responses.clean(seconds_since_start);
|
||||
|
||||
last_pending_scrape_cleaning = now;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use std::collections::BTreeMap;
|
||||
use std::time::Instant;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use slab::Slab;
|
||||
|
||||
use aquatic_common::ValidUntil;
|
||||
use aquatic_common::{SecondsSinceServerStart, ValidUntil};
|
||||
use aquatic_udp_protocol::*;
|
||||
|
||||
use crate::common::*;
|
||||
|
|
@ -97,11 +96,9 @@ impl PendingScrapeResponseSlab {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn clean(&mut self) {
|
||||
let now = Instant::now();
|
||||
|
||||
pub fn clean(&mut self, now: SecondsSinceServerStart) {
|
||||
self.0.retain(|k, v| {
|
||||
if v.valid_until.0 > now {
|
||||
if v.valid_until.valid(now) {
|
||||
true
|
||||
} else {
|
||||
::log::warn!(
|
||||
|
|
@ -120,6 +117,7 @@ impl PendingScrapeResponseSlab {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use aquatic_common::ServerStartInstant;
|
||||
use quickcheck::TestResult;
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
||||
|
|
@ -138,7 +136,7 @@ mod tests {
|
|||
|
||||
config.swarm_workers = swarm_workers as usize;
|
||||
|
||||
let valid_until = ValidUntil::new(1);
|
||||
let valid_until = ValidUntil::new(ServerStartInstant::new(), 1);
|
||||
|
||||
let mut map = PendingScrapeResponseSlab::default();
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use std::sync::atomic::Ordering;
|
|||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use aquatic_common::ServerStartInstant;
|
||||
use crossbeam_channel::Receiver;
|
||||
use rand::{rngs::SmallRng, SeedableRng};
|
||||
|
||||
|
|
@ -21,6 +22,7 @@ pub fn run_swarm_worker(
|
|||
_sentinel: PanicSentinel,
|
||||
config: Config,
|
||||
state: State,
|
||||
server_start_instant: ServerStartInstant,
|
||||
request_receiver: Receiver<(SocketWorkerIndex, ConnectedRequest, CanonicalSocketAddr)>,
|
||||
response_sender: ConnectedResponseSender,
|
||||
worker_index: SwarmWorkerIndex,
|
||||
|
|
@ -29,7 +31,7 @@ pub fn run_swarm_worker(
|
|||
let mut rng = SmallRng::from_entropy();
|
||||
|
||||
let timeout = Duration::from_millis(config.request_channel_recv_timeout_ms);
|
||||
let mut peer_valid_until = ValidUntil::new(config.cleaning.max_peer_age);
|
||||
let mut peer_valid_until = ValidUntil::new(server_start_instant, config.cleaning.max_peer_age);
|
||||
|
||||
let cleaning_interval = Duration::from_secs(config.cleaning.torrent_cleaning_interval);
|
||||
let statistics_update_interval = Duration::from_secs(config.statistics.interval);
|
||||
|
|
@ -81,10 +83,14 @@ pub fn run_swarm_worker(
|
|||
if iter_counter % 128 == 0 {
|
||||
let now = Instant::now();
|
||||
|
||||
peer_valid_until = ValidUntil::new_with_now(now, config.cleaning.max_peer_age);
|
||||
peer_valid_until = ValidUntil::new(server_start_instant, config.cleaning.max_peer_age);
|
||||
|
||||
if now > last_cleaning + cleaning_interval {
|
||||
let (ipv4, ipv6) = torrents.clean_and_get_num_peers(&config, &state.access_list);
|
||||
let (ipv4, ipv6) = torrents.clean_and_get_num_peers(
|
||||
&config,
|
||||
&state.access_list,
|
||||
server_start_instant,
|
||||
);
|
||||
|
||||
if config.statistics.active() {
|
||||
state.statistics_ipv4.peers[worker_index.0].store(ipv4, Ordering::Release);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use std::net::Ipv4Addr;
|
||||
use std::net::Ipv6Addr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use aquatic_common::SecondsSinceServerStart;
|
||||
use aquatic_common::ServerStartInstant;
|
||||
use aquatic_common::{
|
||||
access_list::{create_access_list_cache, AccessListArcSwap, AccessListCache, AccessListMode},
|
||||
extract_response_peers, AmortizedIndexMap, ValidUntil,
|
||||
|
|
@ -99,9 +100,9 @@ impl<I: Ip> TorrentData<I> {
|
|||
}
|
||||
|
||||
/// Remove inactive peers and reclaim space
|
||||
fn clean(&mut self, now: Instant) {
|
||||
fn clean(&mut self, now: SecondsSinceServerStart) {
|
||||
self.peers.retain(|_, peer| {
|
||||
if peer.valid_until.0 > now {
|
||||
if peer.valid_until.valid(now) {
|
||||
true
|
||||
} else {
|
||||
match peer.status {
|
||||
|
|
@ -143,7 +144,7 @@ impl<I: Ip> TorrentMap<I> {
|
|||
&mut self,
|
||||
access_list_cache: &mut AccessListCache,
|
||||
access_list_mode: AccessListMode,
|
||||
now: Instant,
|
||||
now: SecondsSinceServerStart,
|
||||
) -> usize {
|
||||
let mut num_peers = 0;
|
||||
|
||||
|
|
@ -192,10 +193,11 @@ impl TorrentMaps {
|
|||
&mut self,
|
||||
config: &Config,
|
||||
access_list: &Arc<AccessListArcSwap>,
|
||||
server_start_instant: ServerStartInstant,
|
||||
) -> (usize, usize) {
|
||||
let mut cache = create_access_list_cache(access_list);
|
||||
let mode = config.access_list.mode;
|
||||
let now = Instant::now();
|
||||
let now = server_start_instant.seconds_elapsed();
|
||||
|
||||
let ipv4 = self.ipv4.clean_and_get_num_peers(&mut cache, mode, now);
|
||||
let ipv6 = self.ipv6.clean_and_get_num_peers(&mut cache, mode, now);
|
||||
|
|
@ -226,7 +228,7 @@ mod tests {
|
|||
ip_address: Ipv4Addr::from(i.to_be_bytes()),
|
||||
port: Port(1),
|
||||
status: PeerStatus::Leeching,
|
||||
valid_until: ValidUntil::new(0),
|
||||
valid_until: ValidUntil::new(ServerStartInstant::new(), 0),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue