Tokio: Missing doc. No found SO_REUSEADDR solution for TcpListener

Created on 28 Apr 2020  路  3Comments  路  Source: tokio-rs/tokio

Hello.

Tokio version: 0.2.19
Documentation - https://docs.rs/tokio/0.2.19/tokio/net/struct.TcpListener.html

I do not see information about the SO_REUSEADDR option.
Can i think that function
impl TcpListener
pub async fn bind (addr: A) -> Result
automatically sets this option?

The SO_REUSEADDR option is the basis of TCP servers; no industrial server can work without it. The lack of intelligible documentation is a big minus for the library.
The lack of support for the SO_REUSEADDR option is an even bigger minus.

A-tokio C-question M-net T-docs

All 3 comments

Tokio's tcp api is based on the standard library, and std::net::TcpListener does not talk about SO_REUSEADDR either. The current behaviour is to always set SO_REUSEADDR when binding a listener, as Tokio internally uses the v0.6 version of the mio crate, whose TcpListener documentation you can find here.

If you wish to configure the socket beyond what Tokio allows you to, the current solution is to use the socket2 crate to configure it, and then convert it to a Tokio tcp stream. Take a look at my answer to this SO question, which describes how to do this:

async fn connect_bind(bind: SockAddr, connect: SockAddr) -> Result<TcpStream> {
    spawn_blocking(move || {
        let socket = Socket::new(Domain::ipv4(), Type::stream(), None)?;
        socket.bind(&bind)?;
        socket.connect(&connect)?;
        TcpStream::from_std(socket.into_tcp_stream())
    }).await?
}

We are interested in providing a TcpSocket api or something like that, which would allow additional configuration, but the standard library provides no such api, and we have not yet taken the time to design it.

Good. But what about writing in your documentation that the option is set to true so that people don鈥檛 ask unnecessary questions and do not waste time researching. I think I鈥檓 not the only one who has addressed such a question and will continue to asking.

I agree that it should be added to the documentation.

I am not aware of anyone having asked the question before, and a large number of our documentation changes are indeed based on questions we receive. Of course, it is possible that someone was looking for the information, found it elsewhere, and then didn't tell us.

Was this page helpful?
0 / 5 - 0 ratings