Skip to content

Commit

Permalink
wip: use async-lock with an async RefCell
Browse files Browse the repository at this point in the history
  • Loading branch information
gwen-lg committed Jan 14, 2025
1 parent 70956fd commit 960a042
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,6 @@ zero_sized_map_values = "warn"

# eframe = { path = "../../egui/crates/eframe" }
# egui = { path = "../../egui/crates/egui" }

# use local async-lock as dependency of async-std for dev-dependencies, instead of published one
async-lock = { path = "../_crates/async-lock/" }
1 change: 1 addition & 0 deletions puffin_http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include = ["**/*.rs", "Cargo.toml", "README.md"]
[dependencies]
anyhow = "1.0"
async-executor = "1.4"
async-lock = "3.4"
async-net = "2.0.0"
flume = "0.11"
futures-lite = { version = "2.5", default-features = false }
Expand Down
14 changes: 7 additions & 7 deletions puffin_http/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Context as _;
use async_executor::{LocalExecutor, Task};
use async_lock::RefCell;
use async_net::{SocketAddr, TcpListener, TcpStream};

use futures_lite::{future, AsyncWriteExt};
use puffin::{FrameSinkId, GlobalProfiler, ScopeCollection};
use std::{
cell::RefCell,
rc::Rc,
sync::{
atomic::{AtomicUsize, Ordering},
Expand Down Expand Up @@ -382,14 +383,14 @@ impl<'a> PuffinServerConnection<'a> {
// Send all scopes when new client connects.
// TODO: send all previous scopes at connection, not on regular send
//self.send_all_scopes = true;
self.clients.borrow_mut().push(Client {
self.clients.borrow_mut().await.push(Client {
client_addr,
packet_tx: Some(packet_tx),
join_handle: Some(join_handle),
send_all_scopes: true,
});
self.num_clients
.store(self.clients.borrow().len(), Ordering::SeqCst);
.store(self.clients.borrow().await.len(), Ordering::SeqCst);
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
break; // Nothing to do for now.
Expand All @@ -412,7 +413,7 @@ struct PuffinServerSend {

impl PuffinServerSend {
pub async fn send(&mut self, frame: &puffin::FrameData) -> anyhow::Result<()> {
if self.clients.borrow().is_empty() {
if self.clients.borrow().await.is_empty() {
return Ok(());
}
puffin::profile_function!();
Expand Down Expand Up @@ -443,9 +444,8 @@ impl PuffinServerSend {
let packet_all_scopes: Packet = packet_all_scopes.into();

// Send frame to clients, remove disconnected clients and update num_clients var
let clients = self.clients.borrow();
let mut idx_to_remove = Vec::new();
for (idx, client) in clients.iter().enumerate() {
for (idx, client) in self.clients.borrow().await.iter().enumerate() {
let packet = if client.send_all_scopes {
packet_all_scopes.clone()
} else {
Expand All @@ -456,7 +456,7 @@ impl PuffinServerSend {
}
}

let mut clients = self.clients.borrow_mut();
let mut clients = self.clients.borrow_mut().await;
idx_to_remove.iter().rev().for_each(|idx| {
clients.remove(*idx);
});
Expand Down

0 comments on commit 960a042

Please sign in to comment.