From 840f67fc8d98fdfe1dc8da65d27b8278bcc79e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Mon, 25 May 2020 18:04:47 +0200 Subject: [PATCH] aquatic_udp: move some config vars to new substruct, ProtocolConfig --- README.md | 6 ++++-- aquatic_udp/src/lib/config.rs | 32 ++++++++++++++++++++++++-------- aquatic_udp/src/lib/handlers.rs | 6 +++--- aquatic_udp/src/lib/network.rs | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d9ac1ff..ca40d28 100644 --- a/README.md +++ b/README.md @@ -94,11 +94,13 @@ request_workers = 1 [network] address = '0.0.0.0:3000' +socket_recv_buffer_size = 524288 +poll_event_capacity = 4096 + +[protocol] max_scrape_torrents = 255 max_response_peers = 255 peer_announce_interval = 900 -socket_recv_buffer_size = 524288 -poll_event_capacity = 4096 [handlers] max_requests_per_iter = 10000 diff --git a/aquatic_udp/src/lib/config.rs b/aquatic_udp/src/lib/config.rs index 41a05cc..276a313 100644 --- a/aquatic_udp/src/lib/config.rs +++ b/aquatic_udp/src/lib/config.rs @@ -14,6 +14,7 @@ pub struct Config { /// generate responses and send them back to the socket workers. pub request_workers: usize, pub network: NetworkConfig, + pub protocol: ProtocolConfig, pub handlers: HandlerConfig, pub statistics: StatisticsConfig, pub cleaning: CleaningConfig, @@ -26,12 +27,6 @@ pub struct Config { pub struct NetworkConfig { /// Bind to this address pub address: SocketAddr, - /// Maximum number of torrents to accept in scrape request - pub max_scrape_torrents: u8, - /// Maximum number of peers to return in announce response - pub max_response_peers: usize, - /// Ask peers to announce this often (seconds) - pub peer_announce_interval: i32, /// Size of socket recv buffer. Use 0 for OS default. /// /// This setting can have a big impact on dropped packages. It might @@ -51,6 +46,18 @@ pub struct NetworkConfig { } +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(default)] +pub struct ProtocolConfig { + /// Maximum number of torrents to accept in scrape request + pub max_scrape_torrents: u8, + /// Maximum number of peers to return in announce response + pub max_response_peers: usize, + /// Ask peers to announce this often (seconds) + pub peer_announce_interval: i32, +} + + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(default)] pub struct HandlerConfig { @@ -99,6 +106,7 @@ impl Default for Config { socket_workers: 1, request_workers: 1, network: NetworkConfig::default(), + protocol: ProtocolConfig::default(), handlers: HandlerConfig::default(), statistics: StatisticsConfig::default(), cleaning: CleaningConfig::default(), @@ -112,11 +120,19 @@ impl Default for NetworkConfig { fn default() -> Self { Self { address: SocketAddr::from(([0, 0, 0, 0], 3000)), + poll_event_capacity: 4096, + socket_recv_buffer_size: 4096 * 128, + } + } +} + + +impl Default for ProtocolConfig { + fn default() -> Self { + Self { max_scrape_torrents: 255, max_response_peers: 255, peer_announce_interval: 60 * 15, - poll_event_capacity: 4096, - socket_recv_buffer_size: 4096 * 128, } } } diff --git a/aquatic_udp/src/lib/handlers.rs b/aquatic_udp/src/lib/handlers.rs index c1b1ad2..6ba6868 100644 --- a/aquatic_udp/src/lib/handlers.rs +++ b/aquatic_udp/src/lib/handlers.rs @@ -260,7 +260,7 @@ pub fn handle_announce_requests( let response = Response::Announce(AnnounceResponse { transaction_id: request.transaction_id, - announce_interval: AnnounceInterval(config.network.peer_announce_interval), + announce_interval: AnnounceInterval(config.protocol.peer_announce_interval), leechers: NumberOfPeers(torrent_data.num_leechers as i32), seeders: NumberOfPeers(torrent_data.num_seeders as i32), peers: response_peers @@ -311,10 +311,10 @@ fn calc_max_num_peers_to_take( peers_wanted: i32, ) -> usize { if peers_wanted <= 0 { - config.network.max_response_peers as usize + config.protocol.max_response_peers as usize } else { ::std::cmp::min( - config.network.max_response_peers as usize, + config.protocol.max_response_peers as usize, peers_wanted as usize ) } diff --git a/aquatic_udp/src/lib/network.rs b/aquatic_udp/src/lib/network.rs index 3f9bf34..2f4233e 100644 --- a/aquatic_udp/src/lib/network.rs +++ b/aquatic_udp/src/lib/network.rs @@ -141,7 +141,7 @@ fn read_requests( Ok((amt, src)) => { let request = request_from_bytes( &buffer[..amt], - config.network.max_scrape_torrents + config.protocol.max_scrape_torrents ); bytes_received += amt;