如何使用Rust和Tokio构建多个并发服务器?

时间:2017-03-13 09:07:56

标签: concurrency rust rust-tokio

我正在寻找使用Rust和Tokio在不同端口上构建多个并发服务器:

let mut core = Core::new().unwrap();
let handle = core.handle();

// I want to bind to multiple port here if it's possible with simple addresses
let addr = "127.0.0.1:80".parse().unwrap();
let addr2 = "127.0.0.1:443".parse().unwrap();

// Or here if there is a special function on the TcpListener
let sock = TcpListener::bind(&addr, &handle).unwrap();

// Or here if there is a special function on the sock
let server = sock.incoming().for_each(|(client_stream, remote_addr)| {
    // And then retrieve the current port in the callback
    println!("Receive connection on {}!", mysterious_function_to_retrieve_the_port);
    Ok(())
});

core.run(server).unwrap();

Tokio是否有选项可以监听多个端口,还是需要为每个端口创建一个简单的线程并在每个端口中运行Core::new()

感谢rust-scoped-pool,我有:

let pool = Pool::new(2);

let mut listening_on = ["127.0.0.1:80", "127.0.0.1:443"];

pool.scoped(|scope| {
    for address in &mut listening_on {
        scope.execute(move ||{
            let mut core = Core::new().unwrap();
            let handle = core.handle();

            let addr = address.parse().unwrap();
            let sock = TcpListener::bind(&addr, &handle).unwrap();

            let server = sock.incoming().for_each(|(client_stream, remote_addr)| {
                println!("Receive connection on {}!", address);
                Ok(())
            });

            core.run(server).unwrap();
        });
    }
});

rust-scoped-pool是我发现执行多个线程并在产生它们之后永远等待的唯一解决方案。我认为它有效,但我想知道是否存在更简单的解决方案。

2 个答案:

答案 0 :(得分:2)

您可以从一个线程运行多个服务器。 core.run(server).unwrap();只是一种方便的方法,而不是唯一/主要的做事方式。

不是单个ForEach运行完成,而是单独生成每个,然后保持线程活着:

let mut core = Core::new().unwrap();
let handle = core.handle();

// I want to bind to multiple port here if it's possible with simple addresses
let addr = "127.0.0.1:80".parse().unwrap();
let addr2 = "127.0.0.1:443".parse().unwrap();

// Or here if there is a special function on the TcpListener
let sock = TcpListener::bind(&addr, &handle).unwrap();

// Or here if there is a special function on the sock
let server = sock.incoming().for_each(|(client_stream, remote_addr)| {
    // And then retrieve the current port in the callback
    println!("Receive connection on {}!", mysterious_function_to_retrieve_the_port);
    Ok(())
});

handle.spawn(sock);
handle.spawn(server);

loop {
    core.turn(None);
}

答案 1 :(得分:0)

我只想跟进,似乎做事的手动方式比46bit的答案要少(至少在2019年为止)。

let addr1 = "127.0.0.1:80".parse().unwrap();
let addr2 = "127.0.0.1:443".parse().unwrap();

let sock1 = TcpListener::bind(&addr1, &handle).unwrap();
let sock2 = TcpListener::bind(&addr2, &handle).unwrap();

let server1 = sock1.incoming().for_each(|_| Ok(()));
let server2 = sock2.incoming().for_each(|_| Ok(()));

let mut runtime = tokio::runtime::Runtime()::new().unwrap();

runtime.spawn(server1);
runtime.spawn(server2);

runtime.shutdown_on_idle().wait().unwrap();