Distribute accepted tcp connections among threads

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.
https://gist.github.com/alexcrichton/7174bbf6c2359f2b9a2de0d0d3973a09

Here is my working code:
https://gist.github.com/tjamaan/8f7666982faf6b7955b14f7eb3cb3950

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.

1 Like

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

1 Like

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

1 Like

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.

1 Like

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.

2 Likes

That works :smile::+1: