Skip to content

Commit

Permalink
Optimize server listening logic, join_all
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed May 17, 2018
1 parent b4f44f4 commit 1e6bc3b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/relay/tcprelay/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use relay::dns_resolver::resolve;
use relay::socks5::Address;
use relay::tcprelay::crypto_io::{DecryptedRead, EncryptedWrite};

use futures::future::join_all;
use futures::stream::Stream;
use futures::{self, Future};

use tokio;
use tokio::net::{TcpListener, TcpStream};
use tokio_io::io::{ReadHalf, WriteHalf};
use tokio_io::{AsyncRead, IoFuture};
use tokio_io::AsyncRead;

use super::{proxy_handshake, try_timeout, tunnel, DecryptedHalf, EncryptedHalfFut, TcpStreamConnect};

Expand Down Expand Up @@ -163,7 +164,7 @@ fn handle_client(server_cfg: Arc<ServerConfig>,

/// Runs the server
pub fn run(config: Arc<Config>) -> impl Future<Item = (), Error = io::Error> + Send {
let mut fut: Option<IoFuture<()>> = None;
let mut vec_fut = Vec::with_capacity(config.server.len());

for svr_cfg in &config.server {
let listener = {
Expand All @@ -190,11 +191,8 @@ pub fn run(config: Arc<Config>) -> impl Future<Item = (), Error = io::Error> + S
err
});

fut = Some(match fut.take() {
Some(fut) => Box::new(fut.join(listening).map(|_| ())) as IoFuture<()>,
None => Box::new(listening) as IoFuture<()>,
})
vec_fut.push(boxed_future(listening));
}

fut.expect("Must have at least one server")
join_all(vec_fut).map(|_| ())
}

4 comments on commit 1e6bc3b

@quininer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FuturesUnordered 是不是更好?

@zonyitoo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该差不多吧,有什么特别好的地方么?

@quininer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure is optimized to manage a large number of futures. Futures managed by FuturesUnordered will only be polled when they generate notifications. This reduces the required amount of work needed to poll large numbers of futures.

FuturesUnordered 只會 poll 產生通知的 Future,Join 會 poll 所有未完成的 Future。

@zonyitoo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

噢。不错不错。那今晚我换掉

Please sign in to comment.