Tonic: Get the server's port/local_addr

Created on 14 May 2020  路  4Comments  路  Source: hyperium/tonic

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).

crattonic enhancement help wanted

Most helpful comment

https://github.com/hyperium/tonic/blob/f6ecaff0de76de16cb1f680dc3473c8cf2bbaddd/examples/src/autoreload/server.rs#L39-L41

It may helps.

let addr: SocketAddr = "0.0.0.0:0".parse()?;

let mut listener = tokio::net::TcpListener::bind(addr).await?;

All 4 comments

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.

https://github.com/hyperium/tonic/blob/f6ecaff0de76de16cb1f680dc3473c8cf2bbaddd/examples/src/autoreload/server.rs#L39-L41

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LucioFranco picture LucioFranco  路  6Comments

pranjalssh picture pranjalssh  路  4Comments

xmclark picture xmclark  路  10Comments

matthauck picture matthauck  路  6Comments

rokadias picture rokadias  路  4Comments