DEV Community

Discussion on: The worst thing I did in Rust

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Could you please elaborate on unbounded asynchronous channels? They're not described in The Rust Programming Language, I could only find them mentioned in Rust by Example. And by "backpressure" you mean that the channels were steadily growing...? P.S.: I'm a Rust newbie.

Collapse
 
veer66 profile image
Vee Satayamas • Edited

You are right. I created a channel using the std::sync::mpsc::channel function. So the channel can grow until my computer is out of memory. I knew this before I wrote my code. However, after my program became more complex, there were many assumptions why the program took more than 32GB of memory after running for one week. It took me days to figure out that a producer among many of them sent much more data than a consumer can finish processing in time.

These days, I use a bounded channel from the Crossbeam library instead.

use crossbeam::channel;
//...
//...
//...
const LINE_BUF_SIZE: usize = 0x10;
//...
//...
//...
let (line_tx, line_rx) = channel::bounded(LINE_BUF_SIZE);
Enter fullscreen mode Exit fullscreen mode

A bounded channel will block a producer if the channel reaches the limit, and it will unblock if the channel has some space. So the program won't use up the memory.