use channel_tracer::{hook_channel, init};
use tokio::sync::mpsc;
use tokio::time;
#[tokio::main]
async fn main() {
init(5);
let size = 20;
let (tx, mut rx) = mpsc::channel(size);
hook_channel(tx.clone(), "multi_producer_channel", size);
let tx_clone1 = tx.clone();
tokio::spawn(async move {
for i in 0..10 {
tx_clone1.send(format!("Producer1 Message {}", i)).await.unwrap();
time::sleep(time::Duration::from_millis(100)).await;
}
});
let tx_clone2 = tx.clone();
tokio::spawn(async move {
for i in 0..10 {
tx_clone2.send(format!("Producer2 Message {}", i)).await.unwrap();
time::sleep(time::Duration::from_millis(150)).await;
}
});
tokio::spawn(async move {
while let Some(msg) = rx.recv().await {
tracing::debug!("Single Consumer received: {}", msg);
time::sleep(time::Duration::from_millis(200)).await;
}
});
let (tx_burst, mut rx_burst) = mpsc::channel(size);
hook_channel(tx_burst.clone(), "burst_traffic_channel", size);
tokio::spawn(async move {
for i in 0..30 {
tx_burst.send(format!("Burst Message {}", i)).await.unwrap();
time::sleep(time::Duration::from_millis(50)).await;
}
});
tokio::spawn(async move {
time::sleep(time::Duration::from_secs(2)).await;
while let Some(msg) = rx_burst.recv().await {
tracing::debug!("Delayed Consumer received: {}", msg);
}
});
time::sleep(time::Duration::from_secs(10)).await;
}