upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/metrics/connection.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 16:53:03 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-12-11 16:53:03 +0000
commit2a9160836bb87fdea3ae891563b0169c68d1c2ab (patch)
tree583c890687beaf7f380fc0be131bdf17485f06fa /src/metrics/connection.rs
parent52489d3b1a7d79e164b4cc901b53fd06c05ce1b1 (diff)
fix: resolve all fmt and clippy warnings
Main lib (src/): - Add #[allow(dead_code)] for build_info field (stored to prevent Prometheus unregistration) - Add #[allow(dead_code)] for first_seen field (reserved for future rate limiting) - Replace .or_insert_with(RelaySyncNeeds::default) with .or_default() - Replace manual div_ceil implementations with .div_ceil(100) Test code (tests/): - Replace .expect(&format!(...)) with .unwrap_or_else(|_| panic!(...)) - Remove needless borrows in fetch_metrics() calls - Add #[allow(dead_code)] and #[allow(unused_imports)] to test helpers module grasp-audit: - Apply cargo fmt to fix formatting
Diffstat (limited to 'src/metrics/connection.rs')
-rw-r--r--src/metrics/connection.rs58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/metrics/connection.rs b/src/metrics/connection.rs
index 6a7f406..2d42081 100644
--- a/src/metrics/connection.rs
+++ b/src/metrics/connection.rs
@@ -25,7 +25,8 @@ use tracing::warn;
25struct ConnectionInfo { 25struct ConnectionInfo {
26 /// Number of active connections from this IP 26 /// Number of active connections from this IP
27 count: u32, 27 count: u32,
28 /// When the first connection from this IP was established 28 /// When the first connection from this IP was established (for future rate limiting)
29 #[allow(dead_code)]
29 first_seen: Instant, 30 first_seen: Instant,
30 /// Whether this IP has been flagged as potentially abusive 31 /// Whether this IP has been flagged as potentially abusive
31 flagged_as_abuse: bool, 32 flagged_as_abuse: bool,
@@ -48,16 +49,16 @@ struct ConnectionInfo {
48pub struct ConnectionTracker { 49pub struct ConnectionTracker {
49 /// Active connections per IP (INTERNAL ONLY - never exposed to metrics) 50 /// Active connections per IP (INTERNAL ONLY - never exposed to metrics)
50 connections: DashMap<IpAddr, ConnectionInfo>, 51 connections: DashMap<IpAddr, ConnectionInfo>,
51 52
52 /// Threshold for abuse flagging (connections per IP) 53 /// Threshold for abuse flagging (connections per IP)
53 abuse_threshold: u32, 54 abuse_threshold: u32,
54 55
55 /// Prometheus gauge: total active connections 56 /// Prometheus gauge: total active connections
56 active_connections: IntGauge, 57 active_connections: IntGauge,
57 58
58 /// Prometheus gauge: number of unique IPs connected 59 /// Prometheus gauge: number of unique IPs connected
59 unique_ips: IntGauge, 60 unique_ips: IntGauge,
60 61
61 /// Prometheus gauge: number of IPs flagged as potential abusers 62 /// Prometheus gauge: number of IPs flagged as potential abusers
62 flagged_abusers: IntGauge, 63 flagged_abusers: IntGauge,
63} 64}
@@ -70,29 +71,30 @@ impl ConnectionTracker {
70 /// * `abuse_threshold` - Number of connections from a single IP before flagging 71 /// * `abuse_threshold` - Number of connections from a single IP before flagging
71 /// * `registry` - Prometheus registry to register metrics with 72 /// * `registry` - Prometheus registry to register metrics with
72 pub fn new(abuse_threshold: u32, registry: &Registry) -> Self { 73 pub fn new(abuse_threshold: u32, registry: &Registry) -> Self {
73 let active_connections = IntGauge::with_opts( 74 let active_connections = IntGauge::with_opts(Opts::new(
74 Opts::new( 75 "ngit_websocket_connections_active",
75 "ngit_websocket_connections_active", 76 "Current active WebSocket connections",
76 "Current active WebSocket connections", 77 ))
77 ) 78 .unwrap();
78 ).unwrap(); 79 registry
79 registry.register(Box::new(active_connections.clone())).unwrap(); 80 .register(Box::new(active_connections.clone()))
80 81 .unwrap();
81 let unique_ips = IntGauge::with_opts( 82
82 Opts::new( 83 let unique_ips = IntGauge::with_opts(Opts::new(
83 "ngit_websocket_unique_ips", 84 "ngit_websocket_unique_ips",
84 "Number of unique IP addresses connected (NOT the IPs themselves)", 85 "Number of unique IP addresses connected (NOT the IPs themselves)",
85 ) 86 ))
86 ).unwrap(); 87 .unwrap();
87 registry.register(Box::new(unique_ips.clone())).unwrap(); 88 registry.register(Box::new(unique_ips.clone())).unwrap();
88 89
89 let flagged_abusers = IntGauge::with_opts( 90 let flagged_abusers = IntGauge::with_opts(Opts::new(
90 Opts::new( 91 "ngit_websocket_flagged_abusers",
91 "ngit_websocket_flagged_abusers", 92 "Number of IPs exceeding connection threshold",
92 "Number of IPs exceeding connection threshold", 93 ))
93 ) 94 .unwrap();
94 ).unwrap(); 95 registry
95 registry.register(Box::new(flagged_abusers.clone())).unwrap(); 96 .register(Box::new(flagged_abusers.clone()))
97 .unwrap();
96 98
97 Self { 99 Self {
98 connections: DashMap::new(), 100 connections: DashMap::new(),
@@ -140,7 +142,7 @@ impl ConnectionTracker {
140 142
141 // Update Prometheus metrics (aggregate counts only) 143 // Update Prometheus metrics (aggregate counts only)
142 self.active_connections.inc(); 144 self.active_connections.inc();
143 145
144 if is_new_ip { 146 if is_new_ip {
145 self.unique_ips.inc(); 147 self.unique_ips.inc();
146 } 148 }
@@ -334,4 +336,4 @@ mod tests {
334 assert_eq!(tracker.active_connections(), 0); 336 assert_eq!(tracker.active_connections(), 0);
335 assert_eq!(tracker.unique_ip_count(), 0); 337 assert_eq!(tracker.unique_ip_count(), 0);
336 } 338 }
337} \ No newline at end of file 339}