cleanup: use Arc to count open connections

This commit is contained in:
Dietmar Maurer 2020-11-04 06:35:44 +01:00
parent 48aa2b93b7
commit 71f413cd27
1 changed files with 4 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}}; use std::sync::Arc;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
@ -164,7 +164,7 @@ fn accept_connections(
let (sender, receiver) = tokio::sync::mpsc::channel(1024); let (sender, receiver) = tokio::sync::mpsc::channel(1024);
let accept_counter = Arc::new(AtomicUsize::new(0)); let accept_counter = Arc::new(());
const MAX_PENDING_ACCEPTS: usize = 1024; const MAX_PENDING_ACCEPTS: usize = 1024;
@ -180,11 +180,10 @@ fn accept_connections(
let acceptor = Arc::clone(&acceptor); let acceptor = Arc::clone(&acceptor);
let mut sender = sender.clone(); let mut sender = sender.clone();
if accept_counter.load(Ordering::SeqCst) > MAX_PENDING_ACCEPTS { if Arc::strong_count(&accept_counter) > MAX_PENDING_ACCEPTS {
eprintln!("connection rejected - to many open connections"); eprintln!("connection rejected - to many open connections");
continue; continue;
} }
accept_counter.fetch_add(1, Ordering::SeqCst);
let accept_counter = accept_counter.clone(); let accept_counter = accept_counter.clone();
tokio::spawn(async move { tokio::spawn(async move {
@ -207,7 +206,7 @@ fn accept_connections(
} }
} }
accept_counter.fetch_sub(1, Ordering::SeqCst); drop(accept_counter); // decrease reference count
}); });
} }
} }