aquatic_http: improve mio and common request handling code

This commit is contained in:
Joakim Frostegård 2021-10-26 23:40:11 +02:00
parent ea2366c808
commit 4fc1509a79
2 changed files with 13 additions and 7 deletions

View file

@ -171,7 +171,8 @@ pub fn upsert_peer_and_get_response_peers<I: Ip>(
pub fn handle_scrape_request( pub fn handle_scrape_request(
config: &Config, config: &Config,
torrent_maps: &mut TorrentMaps, torrent_maps: &mut TorrentMaps,
(meta, request): (ConnectionMeta, ScrapeRequest), meta: ConnectionMeta,
request: ScrapeRequest,
) -> ScrapeResponse { ) -> ScrapeResponse {
let num_to_take = request let num_to_take = request
.info_hashes .info_hashes

View file

@ -30,7 +30,7 @@ pub fn run_request_worker(
let timeout = Duration::from_micros(config.handlers.channel_recv_timeout_microseconds); let timeout = Duration::from_micros(config.handlers.channel_recv_timeout_microseconds);
loop { loop {
let mut opt_torrent_map_guard: Option<MutexGuard<TorrentMaps>> = None; let mut opt_torrent_maps: Option<MutexGuard<TorrentMaps>> = None;
// If torrent state mutex is locked, just keep collecting requests // If torrent state mutex is locked, just keep collecting requests
// and process them later. This can happen with either multiple // and process them later. This can happen with either multiple
@ -51,7 +51,7 @@ pub fn run_request_worker(
} }
None => { None => {
if let Some(torrent_guard) = state.torrent_maps.try_lock() { if let Some(torrent_guard) = state.torrent_maps.try_lock() {
opt_torrent_map_guard = Some(torrent_guard); opt_torrent_maps = Some(torrent_guard);
break; break;
} }
@ -59,8 +59,8 @@ pub fn run_request_worker(
} }
} }
let mut torrent_map_guard = let mut torrent_maps =
opt_torrent_map_guard.unwrap_or_else(|| state.torrent_maps.lock()); opt_torrent_maps.unwrap_or_else(|| state.torrent_maps.lock());
let valid_until = ValidUntil::new(config.cleaning.max_peer_age); let valid_until = ValidUntil::new(config.cleaning.max_peer_age);
@ -68,7 +68,7 @@ pub fn run_request_worker(
let response = handle_announce_request( let response = handle_announce_request(
&config, &config,
&mut rng, &mut rng,
&mut torrent_map_guard, &mut torrent_maps,
valid_until, valid_until,
meta, meta,
request request
@ -79,7 +79,12 @@ pub fn run_request_worker(
} }
for (meta, request) in scrape_requests.drain(..) { for (meta, request) in scrape_requests.drain(..) {
let response = handle_scrape_request(&config, &mut torrent_map_guard, (meta, request)); let response = handle_scrape_request(
&config,
&mut torrent_maps,
meta,
request
);
response_channel_sender.send(meta, Response::Scrape(response)); response_channel_sender.send(meta, Response::Scrape(response));
wake_socket_workers[meta.worker_index] = true; wake_socket_workers[meta.worker_index] = true;