Closed mariogemoll closed 3 years ago
This is also very useful when there are multiple servers on the same host, where it is usual to let the OS find a free port by specifying :0
.
It may helps.
let addr: SocketAddr = "0.0.0.0:0".parse()?;
let mut listener = tokio::net::TcpListener::bind(addr).await?;
Great, that worked, thanks!
Can we reopen this? Although there is a workaround, it would be great to support this out of the box.
I don't quite see how to add a more convenient way could be added. Once the server (Router
) has been bound to an address or an "incoming" it is no longer accessible. All the methods that do this consume self
.
Hyper's server gets around that by making the server itself a Future
whereas tonic users async fn
s.
I'm personally fine with requiring users to create the TcpListener
to get the address and passing it in with server_with_incoming
.
Yeah, I don't think its worth making things more complicated to support this feature. Going to close for now.
Would it be possible to reopen this issue? I try to find a free port by setting :0
as the port, and I'm looking to find the port it picked.
The workaround in https://github.com/hyperium/tonic/issues/351#issuecomment-638346513 no longer seems to be an option, as incoming
is no longer available on tokio::net::TcpListener
in recent versions of tokio.
Edit: You can use this workaround instead:
let addr: SocketAddr = "127.0.0.1:0".parse()?;
let listener = match tokio::net::TcpListener::bind(addr).await?;
println!("Server listening on {}", listener.local_addr().unwrap());
let stream = tokio_stream::wrappers::TcpListenerStream::new(listener);
server.serve_with_incoming(stream).await?;
Feature Request
Crates
tonic
Motivation
For testing purposes it would be good if there was a way to retrieve the port a server is listening on when it was started without a specified port (ie. with port 0).
Proposal
Add functionality to obtain the port/local_addr a server is listening on (similar to https://docs.rs/hyper/0.13.5/hyper/server/struct.Server.html#method.local_addr).