Skip to content

Commit 4c92dbf

Browse files
committed
add wait_client functionality
use a rendez-vous channel this could be useful for issues #85 and #172
1 parent 65bb1d9 commit 4c92dbf

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

puffin_http/src/server.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct Server {
2424
num_clients: Arc<AtomicUsize>,
2525
stop_connection: Arc<AtomicBool>,
2626
sink_mngr: SinkManager,
27+
wait_client: Receiver<()>,
2728
}
2829

2930
impl Server {
@@ -239,6 +240,7 @@ impl Server {
239240
// but `crossbeam_channel` will continue until the channel is empty.
240241
let (tx, rx): (crossbeam_channel::Sender<Arc<puffin::FrameData>>, _) =
241242
crossbeam_channel::unbounded();
243+
let (client_connected, wait_client) = crossbeam_channel::bounded(0);
242244

243245
let (client_send, client_recv) = crossbeam_channel::unbounded();
244246

@@ -256,7 +258,7 @@ impl Server {
256258
exit,
257259
};
258260

259-
if let Err(err) = ps_connection.accept_new_clients() {
261+
if let Err(err) = ps_connection.accept_new_clients(&client_connected) {
260262
log::warn!("puffin server `accept new clients` failure: {err}");
261263
}
262264

@@ -293,13 +295,24 @@ impl Server {
293295
num_clients,
294296
sink_mngr,
295297
stop_connection,
298+
wait_client,
296299
})
297300
}
298301

299302
/// Number of clients currently connected.
300303
pub fn num_clients(&self) -> usize {
301304
self.num_clients.load(Ordering::SeqCst)
302305
}
306+
307+
/// Block thread to wait at least a puffin client.
308+
///
309+
/// # Errors
310+
/// Return error from channel.
311+
pub fn wait_client(&self) -> Result<(), crossbeam_channel::RecvError> {
312+
puffin::profile_function!();
313+
log::info!("Wait puffin_http client");
314+
self.wait_client.recv()
315+
}
303316
}
304317

305318
impl Drop for Server {
@@ -347,7 +360,7 @@ struct PuffinServerConnection {
347360
}
348361

349362
impl PuffinServerConnection {
350-
fn accept_new_clients(&self) -> anyhow::Result<()> {
363+
fn accept_new_clients(&self, rdv_channel: &Sender<()>) -> anyhow::Result<()> {
351364
loop {
352365
match self.tcp_listener.accept() {
353366
Ok((tcp_stream, client_addr)) => {
@@ -378,6 +391,7 @@ impl PuffinServerConnection {
378391
self.client_send
379392
.send(new_client)
380393
.context("failed to send new client to frame sender")?;
394+
rdv_channel.send(())?;
381395
}
382396
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
383397
if self.exit.load(Ordering::Acquire) {

0 commit comments

Comments
 (0)