Skip to content

Commit 892c767

Browse files
Arquflub
andauthored
fix(iroh-relay): bring back unique node counts (#3197)
## Description This got lost in #3093 and wasn't covered in #3194 ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme) --------- Co-authored-by: Floris Bruynooghe <[email protected]>
1 parent a1a3f57 commit 892c767

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-relay/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ reloadable-state = { version = "0.1", optional = true }
8585
rustls-cert-reloadable-resolver = { version = "0.7.1", optional = true }
8686
rustls-cert-file-reader = { version = "0.4.1", optional = true }
8787
rustls-pemfile = { version = "2.1", optional = true }
88+
time = { version = "0.3.37", optional = true }
8889
tokio-rustls-acme = { version = "0.6", optional = true }
8990
tokio-tungstenite = { version = "0.24", default-features = false, optional = true } # keep version in sync with what tokio-tungstenite-wasm depends on
9091
toml = { version = "0.8", optional = true }
@@ -143,6 +144,7 @@ server = [
143144
"dep:rustls-cert-file-reader",
144145
"dep:rustls-cert-reloadable-resolver",
145146
"dep:rustls-pemfile",
147+
"dep:time",
146148
"dep:tokio-rustls-acme",
147149
"dep:tokio-tungstenite",
148150
"dep:toml",

iroh-relay/src/server/client.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//! The server-side representation of an ongoing client relaying connection.
22
3-
use std::{future::Future, num::NonZeroU32, pin::Pin, sync::Arc, task::Poll, time::Duration};
3+
use std::{
4+
collections::HashSet, future::Future, num::NonZeroU32, pin::Pin, sync::Arc, task::Poll,
5+
time::Duration,
6+
};
47

58
use anyhow::{bail, Context, Result};
69
use bytes::Bytes;
710
use iroh_base::NodeId;
811
use iroh_metrics::{inc, inc_by};
912
use n0_future::{FutureExt, Sink, SinkExt, Stream, StreamExt};
1013
use rand::Rng;
14+
use time::{Date, OffsetDateTime};
1115
use tokio::{
1216
sync::mpsc::{self, error::TrySendError},
1317
time::MissedTickBehavior,
@@ -105,6 +109,7 @@ impl Client {
105109
node_id,
106110
connection_id,
107111
clients: clients.clone(),
112+
client_counter: ClientCounter::default(),
108113
ping_tracker: PingTracker::default(),
109114
};
110115

@@ -205,6 +210,8 @@ struct Actor {
205210
connection_id: u64,
206211
/// Reference to the other connected clients.
207212
clients: Clients,
213+
/// Statistics about the connected clients
214+
client_counter: ClientCounter,
208215
ping_tracker: PingTracker,
209216
}
210217

@@ -214,6 +221,9 @@ impl Actor {
214221
// connection is accepted long before this in the HTTP server, but it is clearer to
215222
// handle the metric here.
216223
inc!(Metrics, accepts);
224+
if self.client_counter.update(self.node_id) {
225+
inc!(Metrics, unique_client_keys);
226+
}
217227
match self.run_inner(done).await {
218228
Err(e) => {
219229
warn!("actor errored {e:#?}, exiting");
@@ -557,6 +567,38 @@ impl Sink<Frame> for RateLimitedRelayedStream {
557567
}
558568
}
559569

570+
/// Tracks how many unique nodes have been seen during the last day.
571+
#[derive(Debug)]
572+
struct ClientCounter {
573+
clients: HashSet<NodeId>,
574+
last_clear_date: Date,
575+
}
576+
577+
impl Default for ClientCounter {
578+
fn default() -> Self {
579+
Self {
580+
clients: HashSet::new(),
581+
last_clear_date: OffsetDateTime::now_utc().date(),
582+
}
583+
}
584+
}
585+
586+
impl ClientCounter {
587+
fn check_and_clear(&mut self) {
588+
let today = OffsetDateTime::now_utc().date();
589+
if today != self.last_clear_date {
590+
self.clients.clear();
591+
self.last_clear_date = today;
592+
}
593+
}
594+
595+
/// Marks this node as seen, returns whether it is new today or not.
596+
fn update(&mut self, client: NodeId) -> bool {
597+
self.check_and_clear();
598+
self.clients.insert(client)
599+
}
600+
}
601+
560602
#[cfg(test)]
561603
mod tests {
562604
use bytes::Bytes;
@@ -595,6 +637,7 @@ mod tests {
595637
connection_id: 0,
596638
node_id,
597639
clients: clients.clone(),
640+
client_counter: ClientCounter::default(),
598641
ping_tracker: PingTracker::default(),
599642
};
600643

0 commit comments

Comments
 (0)