Code Monkey home page Code Monkey logo

response_channel's Introduction

Response Channel

A simple wrapper around the tokio::sync::mpsc channel to provide a bidirectional response channel.

Usually, instantiating a bidrectional channel is cumbersome and requires a lot of boilerplate. response_channel provides a method to create a bidirectional response channel using the same API as creating a regular tokio::sync::mpsc channel (i.e., by just calling a function).

use tokio::task::spawn;
use tokio::join;

type Message = u8;
type Response = u8;

fn main() {
    const BUFFER_SIZE: usize = 10;
    let (mut tx, mut rx) = response_channel::channel::<Message, Response>(BUFFER_SIZE, None);
    let fut1 = spawn(async move {
        for i in 0..10 {
            let response = tx.send_await_automatic(i).await.unwrap().unwrap();
            assert_eq!(response, i + 1);
        };
    });
    let fut2 = spawn(async move {
        while let Some((message, tx)) = rx.recv().await {
            let response = message + 1;
            tx.send(response).await.unwrap();
        };
    });
    let (res1, res2) = join!(fut1, fut2);
    res1.unwrap();
    res2.unwrap();
}

Implementation Notes

Multiple response_channel::Senders can also exist, each one able to send its own messages to the Receiver and receive localized responses! Namely, if one particular response_channel::Sender sends a message to the Receiver, other response_channel::Senders will not be able to see the response.

The response_channel::Sender struct internally contains the forwards transmission line, as well as the reverse transmission and reverse receiving line. The reason why the reverse transmission line needs to be held is so that the response_channel::Sender can send the transmission line as an argument. The Receiver can then take the message (which contains the data and the reverse transmissions line), handle the data, and respond using the given transmission line.

Note that cloning the response_channel::Sender will clone the internal tokio::sync::mpsc::Sender (so that the same Receiver will be hit). It, however, will create a new reverse channel. This makes sense since a new channel is being created, so a new reverse channel needs to be created.

response_channel's People

Contributors

raunakab avatar

Stargazers

Yuefeng Zhu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.