Skip to content

Commit 6c38c6b

Browse files
committed
Show number of connected clients in web interface
1 parent 595fcc8 commit 6c38c6b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,34 @@ fn is_timeout(err: &hyper::Error) -> Option<&ProxyCacheError> {
35393539
}
35403540
}
35413541

3542+
mod client_counter {
3543+
use std::sync::atomic::{AtomicUsize, Ordering};
3544+
3545+
static CONNECTED_CLIENTS: AtomicUsize = AtomicUsize::new(0);
3546+
3547+
#[must_use]
3548+
pub(crate) fn connected_clients() -> usize {
3549+
CONNECTED_CLIENTS.load(Ordering::Relaxed)
3550+
}
3551+
3552+
pub(super) struct ClientCounter {
3553+
_private: (),
3554+
}
3555+
3556+
impl ClientCounter {
3557+
pub(super) fn new() -> Self {
3558+
CONNECTED_CLIENTS.fetch_add(1, Ordering::Relaxed);
3559+
Self { _private: () }
3560+
}
3561+
}
3562+
3563+
impl Drop for ClientCounter {
3564+
fn drop(&mut self) {
3565+
CONNECTED_CLIENTS.fetch_sub(1, Ordering::Relaxed);
3566+
}
3567+
}
3568+
}
3569+
35423570
async fn main_loop() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
35433571
#[cfg(feature = "tokio_console")]
35443572
console_subscriber::init();
@@ -3807,6 +3835,8 @@ async fn main_loop() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
38073835
err
38083836
})?;
38093837

3838+
let client_counter = client_counter::ClientCounter::new();
3839+
38103840
info!("New client connection from {}", client.ip().to_canonical());
38113841
let client_start = Instant::now();
38123842

@@ -3858,6 +3888,8 @@ async fn main_loop() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
38583888
}
38593889
}
38603890

3891+
drop(client_counter);
3892+
38613893
info!(
38623894
"Closed connection to {} after {}",
38633895
client.ip().to_canonical(),

src/web_interface.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::HumanFmt;
2424
use crate::ProxyCacheBody;
2525
use crate::RUNTIMEDETAILS;
2626
use crate::UNCACHEABLES;
27+
use crate::client_counter::connected_clients;
2728
use crate::global_config;
2829
use crate::{APP_NAME, LOGSTORE, database::Database, error::ProxyCacheError, quick_response};
2930

@@ -336,6 +337,7 @@ async fn serve_root(appstate: &AppState) -> Response<ProxyCacheBody> {
336337
<br>Bind Port:&nbsp;&nbsp;{} \
337338
<br>Database Size:&nbsp;&nbsp;{} \
338339
<br>Memory Usage:&nbsp;&nbsp;{}&nbsp;&nbsp;({}) \
340+
<br>Connected Clients:&nbsp;&nbsp;{} \
339341
<br>Active Downloads:&nbsp;&nbsp;{}",
340342
APP_VERSION,
341343
rd.start_time
@@ -355,6 +357,7 @@ async fn serve_root(appstate: &AppState) -> Response<ProxyCacheBody> {
355357
|| String::from("???"),
356358
|ms| HumanFmt::Size(ms.virtual_mem as u64).to_string()
357359
),
360+
connected_clients(),
358361
active_downloads
359362
))
360363
.with_link(

0 commit comments

Comments
 (0)