From c48a83b06a69bde1a34365a3fba507729de1bec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Fri, 2 Feb 2024 13:55:57 +0100 Subject: [PATCH] udp: reorder declarations in common.rs --- crates/udp/src/common.rs | 130 +++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/crates/udp/src/common.rs b/crates/udp/src/common.rs index bc3c3ed..d935944 100644 --- a/crates/udp/src/common.rs +++ b/crates/udp/src/common.rs @@ -16,6 +16,34 @@ use crate::config::Config; pub const BUFFER_SIZE: usize = 8192; +#[derive(Clone, Copy, Debug)] +pub enum IpVersion { + V4, + V6, +} + +#[cfg(feature = "prometheus")] +impl IpVersion { + pub fn prometheus_str(&self) -> &'static str { + match self { + Self::V4 => "4", + Self::V6 => "6", + } + } +} + +#[derive(Clone, Copy, Debug)] +pub struct SocketWorkerIndex(pub usize); + +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +pub struct SwarmWorkerIndex(pub usize); + +impl SwarmWorkerIndex { + pub fn from_info_hash(config: &Config, info_hash: InfoHash) -> Self { + Self(info_hash.0[0] as usize % config.swarm_workers) + } +} + #[derive(Debug)] pub struct PendingScrapeRequest { pub slab_key: usize, @@ -41,18 +69,6 @@ pub enum ConnectedResponse { Scrape(PendingScrapeResponse), } -#[derive(Clone, Copy, Debug)] -pub struct SocketWorkerIndex(pub usize); - -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] -pub struct SwarmWorkerIndex(pub usize); - -impl SwarmWorkerIndex { - pub fn from_info_hash(config: &Config, info_hash: InfoHash) -> Self { - Self(info_hash.0[0] as usize % config.swarm_workers) - } -} - pub struct ConnectedRequestSender { index: SocketWorkerIndex, senders: Vec>, @@ -155,11 +171,38 @@ impl ConnectedResponseSender { pub type ConnectedResponseReceiver = Receiver<(CanonicalSocketAddr, ConnectedResponse)>; -pub enum StatisticsMessage { - Ipv4PeerHistogram(Histogram), - Ipv6PeerHistogram(Histogram), - PeerAdded(PeerId), - PeerRemoved(PeerId), +#[derive(Clone)] +pub struct Statistics { + pub socket: Vec>>, + pub swarm: Vec>>, +} + +impl Statistics { + pub fn new(config: &Config) -> Self { + Self { + socket: repeat_with(Default::default) + .take(config.socket_workers) + .collect(), + swarm: repeat_with(Default::default) + .take(config.swarm_workers) + .collect(), + } + } +} + +#[derive(Default)] +pub struct IpVersionStatistics { + pub ipv4: T, + pub ipv6: T, +} + +impl IpVersionStatistics { + pub fn by_ip_version(&self, ip_version: IpVersion) -> &T { + match ip_version { + IpVersion::V4 => &self.ipv4, + IpVersion::V6 => &self.ipv6, + } + } } #[derive(Default)] @@ -181,54 +224,11 @@ pub struct SwarmWorkerStatistics { pub peers: AtomicUsize, } -#[derive(Clone, Copy, Debug)] -pub enum IpVersion { - V4, - V6, -} - -#[cfg(feature = "prometheus")] -impl IpVersion { - pub fn prometheus_str(&self) -> &'static str { - match self { - Self::V4 => "4", - Self::V6 => "6", - } - } -} - -#[derive(Default)] -pub struct IpVersionStatistics { - pub ipv4: T, - pub ipv6: T, -} - -impl IpVersionStatistics { - pub fn by_ip_version(&self, ip_version: IpVersion) -> &T { - match ip_version { - IpVersion::V4 => &self.ipv4, - IpVersion::V6 => &self.ipv6, - } - } -} - -#[derive(Clone)] -pub struct Statistics { - pub socket: Vec>>, - pub swarm: Vec>>, -} - -impl Statistics { - pub fn new(config: &Config) -> Self { - Self { - socket: repeat_with(Default::default) - .take(config.socket_workers) - .collect(), - swarm: repeat_with(Default::default) - .take(config.swarm_workers) - .collect(), - } - } +pub enum StatisticsMessage { + Ipv4PeerHistogram(Histogram), + Ipv6PeerHistogram(Histogram), + PeerAdded(PeerId), + PeerRemoved(PeerId), } #[derive(Clone)]