use poolshark::global::{GPooled, Pool};
use std::sync::LazyLock;
use tokio::{sync::mpsc, task};
type Batch = Vec<GPooled<String>>;
static STRINGS: LazyLock<Pool<String>> = LazyLock::new(|| Pool::new(1024, 4096));
static BATCHES: LazyLock<Pool<Batch>> = LazyLock::new(|| Pool::new(1024, 1024));
async fn producer(tx: mpsc::Sender<GPooled<Batch>>) {
use std::fmt::Write;
loop {
let mut batch = BATCHES.take();
for _ in 0..100 {
let mut s = STRINGS.take();
write!(s, "very important data").unwrap();
batch.push(s)
}
if let Err(_) = tx.send(batch).await {
break; }
}
}
#[tokio::main(flavor = "multi_thread")]
async fn main() {
let (tx, mut rx) = mpsc::channel(10);
task::spawn(producer(tx));
while let Some(mut batch) = rx.recv().await {
for s in batch.drain(..) {
println!("a message from our sponsor {s}")
}
}
}