Skip to content

Commit 421f121

Browse files
committed
Remove unnecessary arc from OverlayMetrics
1 parent f20500e commit 421f121

File tree

5 files changed

+19
-25
lines changed

5 files changed

+19
-25
lines changed

portalnet/src/metrics/portalnet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::metrics::overlay::OverlayMetrics;
22
use crate::metrics::storage::StorageMetrics;
33
use lazy_static::lazy_static;
4-
use prometheus_exporter::prometheus::{default_registry, Registry};
4+
use prometheus_exporter::prometheus::default_registry;
55

66
// We use lazy_static to ensure that the metrics registry is initialized only once, for each
77
// runtime. This is important because the registry is a global singleton, and if it is
@@ -23,8 +23,8 @@ pub struct PortalnetMetrics {
2323
impl PortalnetMetrics {
2424
pub fn new() -> anyhow::Result<Self> {
2525
let registry = default_registry();
26-
let overlay = OverlayMetrics::new(&registry)?;
27-
let storage = StorageMetrics::new(&registry)?;
26+
let overlay = OverlayMetrics::new(registry)?;
27+
let storage = StorageMetrics::new(registry)?;
2828
Ok(Self { overlay, storage })
2929
}
3030
}

portalnet/src/overlay.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct OverlayProtocol<TContentKey, TMetric, TValidator, TStore> {
113113
/// Accepted content validator that makes requests to this/other overlay networks
114114
validator: Arc<TValidator>,
115115
/// Runtime telemetry metrics for the overlay network.
116-
metrics: Arc<OverlayMetrics>,
116+
metrics: OverlayMetrics,
117117
}
118118

119119
impl<
@@ -141,8 +141,6 @@ where
141141
config.table_filter,
142142
config.bucket_filter,
143143
)));
144-
let metrics = Arc::new(metrics);
145-
146144
let command_tx = OverlayService::<TContentKey, TMetric, TValidator, TStore>::spawn(
147145
Arc::clone(&discovery),
148146
Arc::clone(&store),
@@ -151,7 +149,7 @@ where
151149
config.ping_queue_interval,
152150
protocol.clone(),
153151
Arc::clone(&utp_socket),
154-
Arc::clone(&metrics),
152+
metrics.clone(),
155153
Arc::clone(&validator),
156154
config.query_timeout,
157155
config.query_peer_timeout,

portalnet/src/overlay_service.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub struct OverlayService<TContentKey, TMetric, TValidator, TStore> {
302302
/// Phantom metric (distance function).
303303
phantom_metric: PhantomData<TMetric>,
304304
/// Metrics reporting component
305-
metrics: Arc<OverlayMetrics>,
305+
metrics: OverlayMetrics,
306306
/// Validator for overlay network content.
307307
validator: Arc<TValidator>,
308308
/// A channel that the overlay service emits events on.
@@ -332,7 +332,7 @@ where
332332
ping_queue_interval: Option<Duration>,
333333
protocol: ProtocolId,
334334
utp_socket: Arc<UtpSocket<crate::discovery::UtpEnr>>,
335-
metrics: Arc<OverlayMetrics>,
335+
metrics: OverlayMetrics,
336336
validator: Arc<TValidator>,
337337
query_timeout: Duration,
338338
query_peer_timeout: Duration,
@@ -1117,7 +1117,7 @@ where
11171117
// Wait for an incoming connection with the given CID. Then, write the data
11181118
// over the uTP stream.
11191119
let utp = Arc::clone(&self.utp_socket);
1120-
let metrics = Arc::clone(&self.metrics);
1120+
let metrics = self.metrics.clone();
11211121
let protocol = self.protocol.clone();
11221122
tokio::spawn(async move {
11231123
metrics.report_utp_active_inc(
@@ -1249,7 +1249,7 @@ where
12491249
let kbuckets = Arc::clone(&self.kbuckets);
12501250
let command_tx = self.command_tx.clone();
12511251
let utp = Arc::clone(&self.utp_socket);
1252-
let metrics = Arc::clone(&self.metrics);
1252+
let metrics = self.metrics.clone();
12531253
let protocol = self.protocol.clone();
12541254

12551255
tokio::spawn(async move {
@@ -1526,7 +1526,7 @@ where
15261526
let response_clone = response.clone();
15271527

15281528
let utp = Arc::clone(&self.utp_socket);
1529-
let metrics = Arc::clone(&self.metrics);
1529+
let metrics = self.metrics.clone();
15301530
let protocol = self.protocol.clone();
15311531

15321532
tokio::spawn(async move {
@@ -1612,7 +1612,7 @@ where
16121612
async fn process_accept_utp_payload(
16131613
validator: Arc<TValidator>,
16141614
store: Arc<RwLock<TStore>>,
1615-
metrics: Arc<OverlayMetrics>,
1615+
metrics: OverlayMetrics,
16161616
kbuckets: Arc<RwLock<KBucketsTable<NodeId, Node>>>,
16171617
command_tx: UnboundedSender<OverlayCommand<TContentKey>>,
16181618
content_keys: Vec<TContentKey>,
@@ -1641,7 +1641,7 @@ where
16411641
// - Propagate all validated content
16421642
let validator = Arc::clone(&validator);
16431643
let store = Arc::clone(&store);
1644-
let metrics = Arc::clone(&metrics);
1644+
let metrics = metrics.clone();
16451645
let protocol = protocol.clone();
16461646
tokio::spawn(async move {
16471647
// Validated received content
@@ -1710,7 +1710,7 @@ where
17101710
async fn send_utp_content(
17111711
mut stream: UtpStream<crate::discovery::UtpEnr>,
17121712
content: &[u8],
1713-
metrics: Arc<OverlayMetrics>,
1713+
metrics: OverlayMetrics,
17141714
protocol: ProtocolId,
17151715
) -> anyhow::Result<()> {
17161716
match stream.write(content).await {
@@ -1866,7 +1866,7 @@ where
18661866
responder: Option<oneshot::Sender<RecursiveFindContentResult>>,
18671867
trace: Option<QueryTrace>,
18681868
nodes_to_poke: Vec<NodeId>,
1869-
metrics: Arc<OverlayMetrics>,
1869+
metrics: OverlayMetrics,
18701870
protocol: ProtocolId,
18711871
) {
18721872
let mut content = content;
@@ -2788,7 +2788,6 @@ mod tests {
27882788
let (command_tx, command_rx) = mpsc::unbounded_channel();
27892789
let (response_tx, response_rx) = mpsc::unbounded_channel();
27902790
let metrics = PORTALNET_METRICS.overlay.clone();
2791-
let metrics = Arc::new(metrics);
27922791
let validator = Arc::new(MockValidator {});
27932792

27942793
OverlayService {

portalnet/src/storage.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ pub struct PortalStorageConfig {
176176
pub distance_fn: DistanceFunction,
177177
pub db: Arc<rocksdb::DB>,
178178
pub sql_connection_pool: Pool<SqliteConnectionManager>,
179-
// do we need arc here?
180179
pub metrics: StorageMetrics,
181180
}
182181

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,18 @@ pub async fn run_trin(
4646
let (node_data_dir, private_key) =
4747
configure_node_data_dir(trin_data_dir, trin_config.private_key)?;
4848

49-
// Initialize prometheus metrics
50-
// add metrics registry to configs
51-
// or should we create a new one every time?
52-
if let Some(addr) = trin_config.enable_metrics_with_url {
53-
prometheus_exporter::start(addr)?;
54-
}
55-
5649
let portalnet_config = PortalnetConfig::new(&trin_config, private_key);
5750

5851
// Initialize base discovery protocol
5952
let mut discovery = Discovery::new(portalnet_config.clone())?;
6053
let talk_req_rx = discovery.start().await?;
6154
let discovery = Arc::new(discovery);
6255

56+
// Initialize prometheus metrics
57+
if let Some(addr) = trin_config.enable_metrics_with_url {
58+
prometheus_exporter::start(addr)?;
59+
}
60+
6361
// Initialize and spawn uTP socket
6462
let (utp_talk_reqs_tx, utp_talk_reqs_rx) = mpsc::unbounded_channel();
6563
let discv5_utp_socket = Discv5UdpSocket::new(Arc::clone(&discovery), utp_talk_reqs_rx);

0 commit comments

Comments
 (0)