Skip to content

refactor(portmapper)!: non-global metrics collection #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 44 additions & 127 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion portmapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ derive_more = { version = "1.0.0", features = ["debug", "display", "from", "try_
futures-lite = "2.5"
futures-util = "0.3.25"
igd-next = { version = "0.15.1", features = ["aio_tokio"] }
iroh-metrics = { version = "0.32", default-features = false }
iroh-metrics = { version = "0.34", default-features = false }
libc = "0.2.139"
netwatch = { version = "0.4.0", path = "../netwatch" }
num_enum = "0.7"
Expand Down
12 changes: 8 additions & 4 deletions portmapper/src/current_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use std::{
net::{Ipv4Addr, SocketAddrV4},
num::NonZeroU16,
pin::Pin,
sync::Arc,
task::Poll,
time::Duration,
};

use iroh_metrics::inc;
use tokio::{sync::watch, time};
use tracing::{debug, trace};

use crate::Metrics;

/// This is an implementation detail to facilitate testing.
pub(super) trait Mapping: std::fmt::Debug + Unpin {
fn external(&self) -> (Ipv4Addr, NonZeroU16);
Expand Down Expand Up @@ -73,16 +75,18 @@ pub(super) struct CurrentMapping<M = super::mapping::Mapping> {
/// Waker to ensure this is polled when needed.
#[debug(skip)]
waker: Option<std::task::Waker>,
metrics: Arc<Metrics>,
}

impl<M: Mapping> CurrentMapping<M> {
/// Creates a new [`CurrentMapping`] and returns the watcher over its external address.
pub(super) fn new() -> (Self, watch::Receiver<Option<SocketAddrV4>>) {
pub(super) fn new(metrics: Arc<Metrics>) -> (Self, watch::Receiver<Option<SocketAddrV4>>) {
let (address_tx, address_rx) = watch::channel(None);
let wrapper = CurrentMapping {
mapping: None,
address_tx,
waker: None,
metrics,
};
(wrapper, address_rx)
}
Expand All @@ -108,7 +112,7 @@ impl<M: Mapping> CurrentMapping<M> {
// inform only if this produces a different external address
let update = old_addr != maybe_external_addr;
if update {
inc!(super::Metrics, external_address_updated);
self.metrics.external_address_updated.inc();
};
update
});
Expand Down Expand Up @@ -201,7 +205,7 @@ mod tests {
const TEST_PORT: NonZeroU16 = // SAFETY: it's clearly non zero
unsafe { NonZeroU16::new_unchecked(9586) };
const TEST_IP: std::net::Ipv4Addr = std::net::Ipv4Addr::LOCALHOST;
let (mut c, mut watcher) = CurrentMapping::<M>::new();
let (mut c, mut watcher) = CurrentMapping::<M>::new(Default::default());
let now = std::time::Instant::now();
c.update(Some((TEST_IP, TEST_PORT)));

Expand Down
Loading
Loading