Skip to content

Commit

Permalink
Use now the right combo of permit and drop for connection limits
Browse files Browse the repository at this point in the history
  • Loading branch information
git001 committed Jun 26, 2024
1 parent 2a9c5f1 commit 9adb9b9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
3 changes: 1 addition & 2 deletions container-files/etc/l4p/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ servers:
sni:
www.test1.com: proxy-via
default: echo
# note yet implemented
maxclients: 2
maxclients: 3
via:
*viaanchor

Expand Down
2 changes: 1 addition & 1 deletion src/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) struct Proxy {
pub default_action: String,
pub upstream: HashMap<String, Upstream>,
pub via: ViaUpstream,
pub maxclients: Arc<Semaphore<>>,
pub maxclients: Arc<Semaphore>,
//pub maxclients: usize,
}

Expand Down
11 changes: 5 additions & 6 deletions src/servers/protocol/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ pub(crate) async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn Error>> {
);

// Put the drop inside the tokio::spawn after the call to accept
loop {
// Big thanks to alice from https://users.rust-lang.org/

loop {
let thread_proxy = config.clone();
let permit = config.maxclients.clone().acquire_owned().await.unwrap();
info!("permit.num_permits {:?}", permit.num_permits());


match listener.accept().await {
Err(err) => {
error!("Failed to accept connection: {}", err);
return Err(Box::new(err));
}
Ok((stream, _)) => {
tokio::spawn(async move {
drop(permit);
match accept(stream, thread_proxy).await {
Ok(_) => {
//debug!("Accepted permit {:?}", permit);
debug!("Accepted permit {:?}", permit);
}
Err(err) => {
error!("Relay thread returned an error: {}", err);
}
};
drop(permit);
});
}
}
}
}

async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn Error>> {

if proxy.default_action.contains("health") {
debug!("Health check request")
} else {
Expand Down

0 comments on commit 9adb9b9

Please sign in to comment.