Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a blocking queue mechanism with futures::sync::mpsc::channel?

I am trying to understand how futures::sync::mpsc::Receiver works. In the below example, the receiver thread sleeps for two seconds and the sender sends every second.

I expect that the sender will need to be blocked because of the wait and then send when the buffer is released.

What I see instead is that it is deadlocked after a time. Increasing the buffer of the channel only extends the time until it is blocked.

What should I do to make the sender send data when the buffer is available and put some backpressure to the sender in such cases? futures::sync::mpsc::channel has its own documentation, but I do not understand how to use it properly.

extern crate futures;
extern crate tokio_core;

use std::{thread, time};

use futures::sync::mpsc;
use futures::{Future, Sink, Stream};

use tokio_core::reactor::Core;

#[derive(Debug)]
struct Stats {
    pub success: usize,
    pub failure: usize,
}

fn main() {
    let mut core = Core::new().expect("Failed to create core");
    let remote = core.remote();

    let (tx, rx) = mpsc::channel(1);

    thread::spawn(move || loop {
        let tx = tx.clone();

        let delay = time::Duration::from_secs(1);
        thread::sleep(delay);
        let f = ::futures::done::<(), ()>(Ok(()));

        remote.spawn(|_| {
            f.then(|res| {
                println!("Sending");
                tx.send(res).wait();
                println!("Sent");
                Ok(())
            })
        });
    });

    let mut stats = Stats {
        success: 0,
        failure: 0,
    };

    let f2 = rx.for_each(|res| {
        println!("Received");
        let delay = time::Duration::from_secs(2);
        thread::sleep(delay);

        match res {
            Ok(_) => stats.success += 1,
            Err(_) => stats.failure += 1,
        }
        println!("stats = {:?}", stats);

        Ok(())
    });

    core.run(f2).expect("Core failed to run");
}
like image 650
Akiner Alkan Avatar asked Nov 12 '18 14:11

Akiner Alkan


1 Answers

  1. Never call wait inside of a future. That's blocking, and blocking should never be done inside a future.

  2. Never call sleep inside of a future. That's blocking, and blocking should never be done inside a future.

  3. Channel backpressure is implemented by the fact that send consumes the Sender and returns a future. The future yields the Sender back to you when there is room in the queue.

extern crate futures; // 0.1.25
extern crate tokio; // 0.1.11

use futures::{future, sync::mpsc, Future, Sink, Stream};
use std::time::Duration;
use tokio::timer::Interval;

#[derive(Debug)]
struct Stats {
    pub success: usize,
    pub failure: usize,
}

fn main() {
    tokio::run(future::lazy(|| {
        let (tx, rx) = mpsc::channel::<Result<(), ()>>(1);

        tokio::spawn({
            Interval::new_interval(Duration::from_millis(10))
                .map_err(|e| panic!("Interval error: {}", e))
                .fold(tx, |tx, _| {
                    tx.send(Ok(())).map_err(|e| panic!("Send error: {}", e))
                })
                .map(drop) // discard the tx
        });

        let mut stats = Stats {
            success: 0,
            failure: 0,
        };

        let i = Interval::new_interval(Duration::from_millis(20))
            .map_err(|e| panic!("Interval error: {}", e));

        rx.zip(i).for_each(move |(res, _)| {
            println!("Received");
            match res {
                Ok(_) => stats.success += 1,
                Err(_) => stats.failure += 1,
            }
            println!("stats = {:?}", stats);

            Ok(())
        })
    }));
}
like image 127
Shepmaster Avatar answered Nov 15 '22 09:11

Shepmaster