Skip to content

how to hand tcp request concurrently #3049

Answered by Darksonn
luxl asked this question in Q&A
Discussion options

You must be logged in to vote

There are several options, but here is one:

async fn process(stream: TcpStream, pet: Arc<Mutex<State>>) -> Result<(), Box<dyn Error>> {
    let (write, read) = Framed::new(stream, PetCodec).split(); // futures::stream::StreamExt
    let (send, recv) = tokio::sync::mpsc::channel(16);

    tokio::try_join!(
        async move {
            while let Some(frm) = read.next().await {
                match frm {
                    Ok(frm) => {
                        let send = send.clone();
                        let pet = pet.clone();
                        tokio::spawn(async move {
                            let response = handle_request(frm, pet).await;
                            let _…

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@Darksonn
Comment options

@luxl
Comment options

@luxl
Comment options

@Darksonn
Comment options

@zuston
Comment options

Answer selected by luxl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants