Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
We call the
serve_with_shutdown
method of tonic::transport::server::Router passing a local socket address. If the server is unable to bind the address, that returns an Err (quite quickly). If it is able to bind the address, it returns Ok only much later, when serving is complete.We'd like to know when(/whether) the server has successfully bound the address, i.e. as soon as we know that bind isn't going to fail, and when a client can begin connecting to it (avoiding any race condition).
Solution
The above can be done using the
serve_with_incoming_shutdown
method instead, passing aTcpIncoming
struct - exactly asserve_with_shutdown
does (it first builds the TcpIncoming, which binds the socket and may fail; and then passes that to the serve_with_incoming... method). By performing these two steps itself, the client can detect success/failure of constructing the TcpIncoming before calling the long-running serve... method.Hence, the "solution" is merely to make the TcpIncoming struct public - it is currently
pub(crate)
within tonic::transport::server::incoming - and topub use
rather thanuse
it from tonic::transport::server.Tests
I note the contribution guidelines state "Bug fixes and new features should include tests. I'm not clear whether this constitutes a bug fix or a new feature. Whilst I can imagine a test that started a server and then tried to create a second TcpIncoming on the same socket-address (looking for a failure), that test sounds like a much larger change than the rest of the PR. So, guidance as to what tests might be appropriate would be appreciated :-), thank you!