Distribute accepted tcp connections among threads


#1

I am trying to create a multi-threaded server using Tokio following the example below by @alexcrichton. However I cannot get the server to serve using more than one thread. The threads run but only one thread serves connections. I made a test client that created thousands of connections but I still couldn’t get more than one thread to serve.

Here is my working code:

What can I change in the code to make it serve using more than one thread?

E: I am using Windows and MSVC.
E2: The thread that serves the connections is random every time I run the application.


#2

Try setting reuse_port to true: https://doc.rust-lang.org/net2-rs/net2/struct.TcpBuilder.html#method.reuse_port


#3

Unfortunately reuse_port AFAIK is not implemented on Windows. What I’d recommend for a cross-platform multithreaded server is to have a thread that’s using blocking I/O with std::net::TcpListener and then accepted connections are shipped over to worker threads and bound with TcpStream::from_stream


#4

Whoosh! Completely missed the windows part - sorry @tjamaan. I’d look for a windows analog - not sure if it exists. If not, @alexcrichton’s proposal is probably the way to go.


#5

I’ve been meaning to write this up for awhile, so I’ve uploaded an example to the tokio-core repository with what I’m thinking of.


#6

That works :smile::+1: