mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
split connection and torrent mutexes again
This commit is contained in:
parent
b9a9a82207
commit
ca52b44389
5 changed files with 90 additions and 82 deletions
|
|
@ -126,25 +126,10 @@ pub struct Statistics {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct HandlerData {
|
|
||||||
pub connections: ConnectionMap,
|
|
||||||
pub torrents: TorrentMap,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl Default for HandlerData {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
connections: HashMap::new(),
|
|
||||||
torrents: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub handler_data: Arc<Mutex<HandlerData>>,
|
pub connections: Arc<Mutex<ConnectionMap>>,
|
||||||
|
pub torrents: Arc<Mutex<TorrentMap>>,
|
||||||
pub statistics: Arc<Statistics>,
|
pub statistics: Arc<Statistics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,7 +137,8 @@ pub struct State {
|
||||||
impl State {
|
impl State {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
handler_data: Arc::new(Mutex::new(HandlerData::default())),
|
connections: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
torrents: Arc::new(Mutex::new(HashMap::new())),
|
||||||
statistics: Arc::new(Statistics::default()),
|
statistics: Arc::new(Statistics::default()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,13 +33,13 @@ pub fn run_request_worker(
|
||||||
);
|
);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut opt_data = None;
|
let mut opt_connections = None;
|
||||||
|
|
||||||
// Collect requests from channel, divide them by type
|
// Collect requests from channel, divide them by type
|
||||||
//
|
//
|
||||||
// Collect a maximum number of request. Stop collecting before that
|
// Collect a maximum number of request. Stop collecting before that
|
||||||
// number is reached if having waited for too long for a request, but
|
// number is reached if having waited for too long for a request, but
|
||||||
// only if HandlerData mutex isn't locked.
|
// only if ConnectionMap mutex isn't locked.
|
||||||
for i in 0..config.handlers.max_requests_per_iter {
|
for i in 0..config.handlers.max_requests_per_iter {
|
||||||
let (request, src): (Request, SocketAddr) = if i == 0 {
|
let (request, src): (Request, SocketAddr) = if i == 0 {
|
||||||
match request_receiver.recv(){
|
match request_receiver.recv(){
|
||||||
|
|
@ -50,8 +50,8 @@ pub fn run_request_worker(
|
||||||
match request_receiver.recv_timeout(timeout){
|
match request_receiver.recv_timeout(timeout){
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
if let Some(data) = state.handler_data.try_lock(){
|
if let Some(guard) = state.connections.try_lock(){
|
||||||
opt_data = Some(data);
|
opt_connections = Some(guard);
|
||||||
|
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -74,32 +74,76 @@ pub fn run_request_worker(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut data: MutexGuard<HandlerData> = opt_data.unwrap_or_else(||
|
let mut connections: MutexGuard<ConnectionMap> = opt_connections.unwrap_or_else(||
|
||||||
state.handler_data.lock()
|
state.connections.lock()
|
||||||
);
|
);
|
||||||
|
|
||||||
handle_connect_requests(
|
handle_connect_requests(
|
||||||
&config,
|
&config,
|
||||||
&mut data,
|
&mut connections,
|
||||||
&mut std_rng,
|
&mut std_rng,
|
||||||
connect_requests.drain(..),
|
connect_requests.drain(..),
|
||||||
&mut responses
|
&mut responses
|
||||||
);
|
);
|
||||||
|
|
||||||
handle_announce_requests(
|
announce_requests.retain(|(request, src)| {
|
||||||
&config,
|
let connection_key = ConnectionKey {
|
||||||
&mut data,
|
connection_id: request.connection_id,
|
||||||
&mut small_rng,
|
socket_addr: *src,
|
||||||
announce_requests.drain(..),
|
};
|
||||||
&mut responses
|
|
||||||
);
|
if connections.contains_key(&connection_key){
|
||||||
handle_scrape_requests(
|
true
|
||||||
&mut data,
|
} else {
|
||||||
scrape_requests.drain(..),
|
let response = ErrorResponse {
|
||||||
&mut responses
|
transaction_id: request.transaction_id,
|
||||||
);
|
message: "Connection invalid or expired".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
responses.push((response.into(), *src));
|
||||||
|
|
||||||
::std::mem::drop(data);
|
false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scrape_requests.retain(|(request, src)| {
|
||||||
|
let connection_key = ConnectionKey {
|
||||||
|
connection_id: request.connection_id,
|
||||||
|
socket_addr: *src,
|
||||||
|
};
|
||||||
|
|
||||||
|
if connections.contains_key(&connection_key){
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
let response = ErrorResponse {
|
||||||
|
transaction_id: request.transaction_id,
|
||||||
|
message: "Connection invalid or expired".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
responses.push((response.into(), *src));
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
::std::mem::drop(connections);
|
||||||
|
|
||||||
|
if !(announce_requests.is_empty() && scrape_requests.is_empty()){
|
||||||
|
let mut torrents = state.torrents.lock();
|
||||||
|
|
||||||
|
handle_announce_requests(
|
||||||
|
&config,
|
||||||
|
&mut torrents,
|
||||||
|
&mut small_rng,
|
||||||
|
announce_requests.drain(..),
|
||||||
|
&mut responses
|
||||||
|
);
|
||||||
|
handle_scrape_requests(
|
||||||
|
&mut torrents,
|
||||||
|
scrape_requests.drain(..),
|
||||||
|
&mut responses
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
for r in responses.drain(..){
|
for r in responses.drain(..){
|
||||||
if let Err(err) = response_sender.send(r){
|
if let Err(err) = response_sender.send(r){
|
||||||
|
|
@ -113,7 +157,7 @@ pub fn run_request_worker(
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn handle_connect_requests(
|
pub fn handle_connect_requests(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
data: &mut MutexGuard<HandlerData>,
|
connections: &mut MutexGuard<ConnectionMap>,
|
||||||
rng: &mut StdRng,
|
rng: &mut StdRng,
|
||||||
requests: Drain<(ConnectRequest, SocketAddr)>,
|
requests: Drain<(ConnectRequest, SocketAddr)>,
|
||||||
responses: &mut Vec<(Response, SocketAddr)>,
|
responses: &mut Vec<(Response, SocketAddr)>,
|
||||||
|
|
@ -128,7 +172,7 @@ pub fn handle_connect_requests(
|
||||||
socket_addr: src,
|
socket_addr: src,
|
||||||
};
|
};
|
||||||
|
|
||||||
data.connections.insert(key, valid_until);
|
connections.insert(key, valid_until);
|
||||||
|
|
||||||
let response = Response::Connect(
|
let response = Response::Connect(
|
||||||
ConnectResponse {
|
ConnectResponse {
|
||||||
|
|
@ -145,7 +189,7 @@ pub fn handle_connect_requests(
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn handle_announce_requests(
|
pub fn handle_announce_requests(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
data: &mut MutexGuard<HandlerData>,
|
torrents: &mut MutexGuard<TorrentMap>,
|
||||||
rng: &mut SmallRng,
|
rng: &mut SmallRng,
|
||||||
requests: Drain<(AnnounceRequest, SocketAddr)>,
|
requests: Drain<(AnnounceRequest, SocketAddr)>,
|
||||||
responses: &mut Vec<(Response, SocketAddr)>,
|
responses: &mut Vec<(Response, SocketAddr)>,
|
||||||
|
|
@ -153,20 +197,6 @@ pub fn handle_announce_requests(
|
||||||
let peer_valid_until = ValidUntil::new(config.cleaning.max_peer_age);
|
let peer_valid_until = ValidUntil::new(config.cleaning.max_peer_age);
|
||||||
|
|
||||||
responses.extend(requests.map(|(request, src)| {
|
responses.extend(requests.map(|(request, src)| {
|
||||||
let connection_key = ConnectionKey {
|
|
||||||
connection_id: request.connection_id,
|
|
||||||
socket_addr: src,
|
|
||||||
};
|
|
||||||
|
|
||||||
if !data.connections.contains_key(&connection_key){
|
|
||||||
let response = ErrorResponse {
|
|
||||||
transaction_id: request.transaction_id,
|
|
||||||
message: "Connection invalid or expired".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
return (response.into(), src);
|
|
||||||
}
|
|
||||||
|
|
||||||
let peer_ip = src.ip();
|
let peer_ip = src.ip();
|
||||||
|
|
||||||
let peer_key = PeerMapKey {
|
let peer_key = PeerMapKey {
|
||||||
|
|
@ -186,7 +216,7 @@ pub fn handle_announce_requests(
|
||||||
valid_until: peer_valid_until,
|
valid_until: peer_valid_until,
|
||||||
};
|
};
|
||||||
|
|
||||||
let torrent_data = data.torrents
|
let torrent_data = torrents
|
||||||
.entry(request.info_hash)
|
.entry(request.info_hash)
|
||||||
.or_default();
|
.or_default();
|
||||||
|
|
||||||
|
|
@ -242,33 +272,19 @@ pub fn handle_announce_requests(
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn handle_scrape_requests(
|
pub fn handle_scrape_requests(
|
||||||
data: &mut MutexGuard<HandlerData>,
|
torrents: &mut MutexGuard<TorrentMap>,
|
||||||
requests: Drain<(ScrapeRequest, SocketAddr)>,
|
requests: Drain<(ScrapeRequest, SocketAddr)>,
|
||||||
responses: &mut Vec<(Response, SocketAddr)>,
|
responses: &mut Vec<(Response, SocketAddr)>,
|
||||||
){
|
){
|
||||||
let empty_stats = create_torrent_scrape_statistics(0, 0);
|
let empty_stats = create_torrent_scrape_statistics(0, 0);
|
||||||
|
|
||||||
responses.extend(requests.map(|(request, src)|{
|
responses.extend(requests.map(|(request, src)|{
|
||||||
let connection_key = ConnectionKey {
|
|
||||||
connection_id: request.connection_id,
|
|
||||||
socket_addr: src,
|
|
||||||
};
|
|
||||||
|
|
||||||
if !data.connections.contains_key(&connection_key){
|
|
||||||
let response = ErrorResponse {
|
|
||||||
transaction_id: request.transaction_id,
|
|
||||||
message: "Connection invalid or expired".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
return (response.into(), src);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut stats: Vec<TorrentScrapeStatistics> = Vec::with_capacity(
|
let mut stats: Vec<TorrentScrapeStatistics> = Vec::with_capacity(
|
||||||
request.info_hashes.len()
|
request.info_hashes.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
for info_hash in request.info_hashes.iter() {
|
for info_hash in request.info_hashes.iter() {
|
||||||
if let Some(torrent_data) = data.torrents.get(info_hash){
|
if let Some(torrent_data) = torrents.get(info_hash){
|
||||||
stats.push(create_torrent_scrape_statistics(
|
stats.push(create_torrent_scrape_statistics(
|
||||||
torrent_data.num_seeders.load(Ordering::SeqCst) as i32,
|
torrent_data.num_seeders.load(Ordering::SeqCst) as i32,
|
||||||
torrent_data.num_leechers.load(Ordering::SeqCst) as i32,
|
torrent_data.num_leechers.load(Ordering::SeqCst) as i32,
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,16 @@ use crate::config::Config;
|
||||||
pub fn clean_connections_and_torrents(state: &State){
|
pub fn clean_connections_and_torrents(state: &State){
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
||||||
let mut data = state.handler_data.lock();
|
let mut connections = state.connections.lock();
|
||||||
|
|
||||||
data.connections.retain(|_, v| v.0 > now);
|
connections.retain(|_, v| v.0 > now);
|
||||||
data.connections.shrink_to_fit();
|
connections.shrink_to_fit();
|
||||||
|
|
||||||
data.torrents.retain(|_, torrent| {
|
::std::mem::drop(connections);
|
||||||
|
|
||||||
|
let mut torrents = state.torrents.lock();
|
||||||
|
|
||||||
|
torrents.retain(|_, torrent| {
|
||||||
let num_seeders = &torrent.num_seeders;
|
let num_seeders = &torrent.num_seeders;
|
||||||
let num_leechers = &torrent.num_leechers;
|
let num_leechers = &torrent.num_leechers;
|
||||||
|
|
||||||
|
|
@ -40,7 +44,7 @@ pub fn clean_connections_and_torrents(state: &State){
|
||||||
!torrent.peers.is_empty()
|
!torrent.peers.is_empty()
|
||||||
});
|
});
|
||||||
|
|
||||||
data.torrents.shrink_to_fit();
|
torrents.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -87,7 +91,7 @@ pub fn gather_and_print_statistics(
|
||||||
|
|
||||||
let mut peers_per_torrent = Histogram::new();
|
let mut peers_per_torrent = Histogram::new();
|
||||||
|
|
||||||
let torrents = &mut state.handler_data.lock().torrents;
|
let torrents = &mut state.torrents.lock();
|
||||||
|
|
||||||
for torrent in torrents.values(){
|
for torrent in torrents.values(){
|
||||||
let num_seeders = torrent.num_seeders.load(Ordering::SeqCst);
|
let num_seeders = torrent.num_seeders.load(Ordering::SeqCst);
|
||||||
|
|
@ -100,6 +104,8 @@ pub fn gather_and_print_statistics(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::std::mem::drop(torrents);
|
||||||
|
|
||||||
if peers_per_torrent.entries() != 0 {
|
if peers_per_torrent.entries() != 0 {
|
||||||
println!(
|
println!(
|
||||||
"peers per torrent: min: {}, p50: {}, p75: {}, p90: {}, p99: {}, p999: {}, max: {}",
|
"peers per torrent: min: {}, p50: {}, p75: {}, p90: {}, p99: {}, p999: {}, max: {}",
|
||||||
|
|
|
||||||
|
|
@ -95,9 +95,9 @@ pub fn create_requests(
|
||||||
|
|
||||||
let mut requests = Vec::new();
|
let mut requests = Vec::new();
|
||||||
|
|
||||||
let d = state.handler_data.lock();
|
let connections = state.connections.lock();
|
||||||
|
|
||||||
let connection_keys: Vec<ConnectionKey> = d.connections.keys()
|
let connection_keys: Vec<ConnectionKey> = connections.keys()
|
||||||
.take(number)
|
.take(number)
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
||||||
|
|
@ -96,9 +96,9 @@ pub fn create_requests(
|
||||||
|
|
||||||
let max_index = info_hashes.len() - 1;
|
let max_index = info_hashes.len() - 1;
|
||||||
|
|
||||||
let d = state.handler_data.lock();
|
let connections = state.connections.lock();
|
||||||
|
|
||||||
let connection_keys: Vec<ConnectionKey> = d.connections.keys()
|
let connection_keys: Vec<ConnectionKey> = connections.keys()
|
||||||
.take(number)
|
.take(number)
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue