Skip to content

Commit 2a9c5f1

Browse files
committed
Try to add Arc Semaphore to limit clients
1 parent 22b36b5 commit 2a9c5f1

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/servers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) struct Proxy {
3232
pub default_action: String,
3333
pub upstream: HashMap<String, Upstream>,
3434
pub via: ViaUpstream,
35-
pub maxclients: Arc<Semaphore>,
35+
pub maxclients: Arc<Semaphore<>>,
3636
//pub maxclients: usize,
3737
}
3838

src/servers/protocol/tcp.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
1616
config.name, config.maxclients
1717
);
1818

19+
// Put the drop inside the tokio::spawn after the call to accept
1920
loop {
21+
2022
let thread_proxy = config.clone();
23+
let permit = config.maxclients.clone().acquire_owned().await.unwrap();
24+
info!("permit.num_permits {:?}", permit.num_permits());
25+
2126
match listener.accept().await {
2227
Err(err) => {
2328
error!("Failed to accept connection: {}", err);
2429
return Err(Box::new(err));
2530
}
2631
Ok((stream, _)) => {
2732
tokio::spawn(async move {
33+
drop(permit);
2834
match accept(stream, thread_proxy).await {
2935
Ok(_) => {
3036
//debug!("Accepted permit {:?}", permit);
@@ -40,8 +46,6 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
4046
}
4147

4248
async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
43-
//let permit = proxy.maxclients.clone().acquire_owned().await.unwrap();
44-
//info!("permit.num_permits {:?}", permit.num_permits());
4549

4650
if proxy.default_action.contains("health") {
4751
debug!("Health check request")

0 commit comments

Comments
 (0)