Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling error while processing stream. #94

Open
alishir opened this issue Dec 17, 2018 · 0 comments
Open

Handling error while processing stream. #94

alishir opened this issue Dec 17, 2018 · 0 comments

Comments

@alishir
Copy link

alishir commented Dec 17, 2018

I have a simple stream of TcpStream::connect futures, I want to process elements in stream concurrently so I used buffer_unordered, but I coundn't figure out how should I create future from new stream to run with tokio::run. Also some connection may be refused because of network condition, so I coulnd't use for_each over stream.

Here is the code so far:

extern crate bytes;
extern crate tokio;

use bytes::BytesMut;
use tokio::io;
use tokio::net::TcpStream;
use tokio::prelude::*;

fn main() {
    let srv_list = vec![
        "127.0.0.1:12345",
        "127.0.0.1:12346",
        "127.0.0.1:12347",
        "127.0.0.1:12348",
        "127.0.0.1:12349",
    ];
    let conn_fut: Vec<_> = srv_list
        .iter()
        .map(|addr| {
            let addr = addr.parse().unwrap();

            TcpStream::connect(&addr)
                .and_then(move |stream| {
                    println!("connected to address: {:?}", addr);
                })
                .map_err(|err| {
                    println!("couldn't connect, {:?}", err);
                })
        })
        .collect();

    let task = stream::iter_ok::<_, ()>(conn_fut);
    let task = task.buffer_unordered(5);
    /*
     what should I do here?!
   */
    tokio::run(task);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant