How to loop the same connection in tokio?

#1

I’m writing a simple Memcached server.
My server code looks like this

    let server = listener
        .incoming()
        .for_each(move |socket| {
            let (reader, writer) = socket.split();
            let shard_cache = shard_cache.clone();
            let val = Vec::new();
            let u = tokio::io::read_until(std::io::BufReader::new(reader), 10, val).and_then(
                move |(stream, val)| {
                    shard_cache.execute(
                        parser::parse_command(val).unwrap(),
                        stream,
                        std::io::BufWriter::new(writer),
                    );
                    Ok(())
                },
            );
            let msg = u.then(|_a| Ok(()));
            tokio::spawn(msg);
            Ok(())
        })

execute method sends the reader and writer to the another thread where I handle the GET and SET operation one by one. After the operation it drops the reader and writer(’'I think connection closes after droping. I’m not sure"). I want a way to loop the connection again to listen for the command and to respond them.
Is there any easiest way to do in Tokio?

0 Likes