Skip to content

Commit 255f23b

Browse files
committed
Change connection limit example to actually limit connections, not get requests.
1 parent f399e2b commit 255f23b

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

examples/limit.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,40 +190,30 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
190190
// based on the current connection count if we want to accept or reject.
191191
// We also want detailed logging of events for the get request, so we can
192192
// detect when the request is finished one way or another.
193-
get: RequestMode::RequestLog,
193+
connected: ConnectMode::Request,
194194
..EventMask::DEFAULT
195195
};
196196
let (tx, mut rx) = EventSender::channel(32, mask);
197197
n0_future::task::spawn(async move {
198198
let requests = ConnectionCounter::new(max_connections);
199199
while let Some(msg) = rx.recv().await {
200-
if let ProviderMessage::GetRequestReceived(mut msg) = msg {
201-
let connection_id = msg.connection_id;
202-
let request_id = msg.request_id;
203-
let res = requests.inc();
204-
match res {
205-
Ok(n) => {
206-
println!("Accepting request {n}, id ({connection_id},{request_id})");
207-
msg.tx.send(Ok(())).await.ok();
208-
}
209-
Err(_) => {
210-
println!(
211-
"Connection limit of {max_connections} exceeded, rejecting request"
212-
);
213-
msg.tx.send(Err(AbortReason::RateLimited)).await.ok();
214-
continue;
215-
}
200+
match msg {
201+
ProviderMessage::ClientConnected(msg) => {
202+
let connection_id = msg.connection_id;
203+
let node_id = msg.node_id;
204+
let res = if let Ok(n) = requests.inc() {
205+
println!("Accepting connection {n}, node_id {node_id}, connection_id {connection_id}");
206+
Ok(())
207+
} else {
208+
Err(AbortReason::RateLimited)
209+
};
210+
msg.tx.send(res).await.ok();
216211
}
217-
let requests = requests.clone();
218-
n0_future::task::spawn(async move {
219-
// just drain the per request events
220-
//
221-
// Note that we have requested updates for the request, now we also need to process them
222-
// otherwise the request will be aborted!
223-
while let Ok(Some(_)) = msg.rx.recv().await {}
224-
println!("Stopping request, id ({connection_id},{request_id})");
212+
ProviderMessage::ConnectionClosed(msg) => {
225213
requests.dec();
226-
});
214+
println!("Connection closed, connection_id {}", msg.connection_id,);
215+
}
216+
_ => {}
227217
}
228218
}
229219
});

src/provider/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl EventSender {
401401
Ok(())
402402
}
403403

404-
/// A new client has been connected.
404+
/// A connection has been closed.
405405
pub async fn connection_closed(&self, f: impl Fn() -> ConnectionClosed) -> ClientResult {
406406
if let Some(client) = &self.inner {
407407
client.notify(f()).await?;

0 commit comments

Comments
 (0)