Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix limiter connections #46

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ data:

[[tiers]]
name = "1"
max_connections = 1
max_connections = 2
[[tiers.rates]]
interval = "1s"
limit = 1024
Expand Down
19 changes: 14 additions & 5 deletions proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,30 @@ pub struct Consumer {
active_connections: usize,
}
impl Consumer {
pub async fn inc_connections(&mut self, state: Arc<State>) {
self.active_connections += 1;
pub async fn inc_connections(&self, state: Arc<State>) {
state
.consumers
.write()
.await
.insert(self.key.clone(), self.clone());
.entry(self.key.clone())
.and_modify(|consumer| consumer.active_connections += 1);
}
pub async fn dec_connections(&mut self, state: Arc<State>) {
self.active_connections -= 1;
state
.consumers
.write()
.await
.insert(self.key.clone(), self.clone());
.entry(self.key.clone())
.and_modify(|consumer| consumer.active_connections -= 1);
}
pub async fn get_active_connections(&self, state: Arc<State>) -> usize {
state
.consumers
.read()
.await
.get(&self.key)
.map(|consumer| consumer.active_connections)
.unwrap_or_default()
}
}
impl Display for Consumer {
Expand Down
13 changes: 7 additions & 6 deletions proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ impl ProxyApp {

match event {
DuplexEvent::ClientRead(0) | DuplexEvent::InstanceRead(0) => {
info!(
consumer = ctx.consumer.to_string(),
active_connections = ctx.consumer.active_connections,
"client disconnected"
);

ctx.consumer.dec_connections(self.state.clone()).await;
state.metrics.dec_total_connections(
&ctx.consumer,
&ctx.namespace,
&ctx.instance,
);

let active_connections =
ctx.consumer.get_active_connections(state.clone()).await;
info!(
consumer = ctx.consumer.to_string(),
active_connections, "client disconnected"
);
return Ok(());
}
DuplexEvent::ClientRead(bytes) => {
Expand Down
Loading