Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to access the port a GRPC server is listening on #3107

Merged
merged 2 commits into from
Feb 8, 2023

Conversation

eranrund
Copy link
Contributor

@eranrund eranrund commented Feb 8, 2023

Motivation

It used to be the case (prior to some grpcio version bump) that it was easy to get the addresses a GRPC server is listening on after the server is created. That was useful when asked to listen on port 0, which causes the system to bind some random free port number. This is a functionality we often use in tests to avoid hard-coding port numbers.

I'd like to make it possible to reuse the TLS credentials setup code while still making it possible to get the chosen port. The scenario that makes me want it is the following:

  1. I have an app object, that amongst other things, creates a GRPC server and configures it to listen on some host:port using a URI passed to it on its initialization (and this Uri could either imply TLS or no TLS)
  2. I want to write a unit test for this object and for that I need to know which port it listens on, which is currently impossible if I pass it a Uri with port 0. Right now, if I want to work around this, I need to duplicate the ServerCredentials creation code that takes a Uri and figured out what to do with it.

Another thing I ran into, is if the URIs are passed using our Uri object, I have no way to change the port. This is useful when for example I pass "insecure-service://127.0.0.1:0/" as the listening uri to this app server object mentioned above. It would then be nice to have a method such as .get_listen_uri() that will then return "insecure-service://127.0.0.1:12345", where 12345 is the port that was randomly chosen. This is useful, since this can then be easily passed into a Client objects (e.g. https://github.com/mobilecoinfoundation/mobilecoin/blob/master/fog/view/connection/src/lib.rs#L48, a pattern repeats itself in pretty much all of our GRPC client objects).


impl ConnectionUriGrpcioServer for ServerBuilder {
fn build_using_uri(self, uri: &impl ConnectionUri, logger: Logger) -> Result<Server> {
/// Get ServerCredentials from a URI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Like the break out to a separate method for this behavior

@eranrund eranrund merged commit 0ac1957 into release/v4.1 Feb 8, 2023
@eranrund eranrund deleted the eran/expose-grpc-server-port branch February 8, 2023 17:00
briancorbin pushed a commit that referenced this pull request Feb 15, 2023
)

* break api into two methods so that it can easily be reused when we want to get the listening port

* add utility method for changing the port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants