Skip to content

Commit

Permalink
Make it possible to access the port a GRPC server is listening on (#3107
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
eranrund authored Feb 8, 2023
1 parent 44fba8f commit 0ac1957
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
37 changes: 21 additions & 16 deletions util/grpc/src/grpcio_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ pub trait ConnectionUriGrpcioServer {
/// Set the channel args to our defaults.
#[must_use]
fn set_default_channel_args(self, env: Arc<Environment>) -> Self;
}

impl ConnectionUriGrpcioServer for ServerBuilder {
fn build_using_uri(self, uri: &impl ConnectionUri, logger: Logger) -> Result<Server> {
/// Get ServerCredentials from a URI
fn server_credentials_from_uri(uri: &impl ConnectionUri, logger: &Logger) -> ServerCredentials {
if uri.use_tls() {
let tls_chain_path = uri
.tls_chain_path()
Expand All @@ -89,33 +88,39 @@ impl ConnectionUriGrpcioServer for ServerBuilder {
let reloader = ServerCertReloader::new(&tls_chain_path, &tls_key_path, logger.clone())
.expect("Failed creating ServerCertReloader");

ServerCredentials::with_fetcher(
Box::new(reloader),
CertificateRequestType::DontRequestClientCertificate,
)
} else {
ServerCredentials::insecure()
}
}
}

impl ConnectionUriGrpcioServer for ServerBuilder {
fn build_using_uri(self, uri: &impl ConnectionUri, logger: Logger) -> Result<Server> {
let server_creds = Self::server_credentials_from_uri(uri, &logger);

if uri.use_tls() {
log::debug!(
logger,
"Binding secure gRPC server to {}:{}",
uri.host(),
uri.port(),
);

let server_creds = ServerCredentials::with_fetcher(
Box::new(reloader),
CertificateRequestType::DontRequestClientCertificate,
);

let mut server = self.build()?;
server.add_listening_port(uri.addr(), server_creds)?;
Ok(server)
} else {
log::warn!(
logger,
"Binding insecure gRPC server to {}:{}",
uri.host(),
uri.port(),
);

let mut server = self.build()?;
server.add_listening_port(uri.addr(), ServerCredentials::insecure())?;
Ok(server)
}

let mut server = self.build()?;
server.add_listening_port(uri.addr(), server_creds)?;
Ok(server)
}

/// Set the channel args to our defaults.
Expand Down
10 changes: 10 additions & 0 deletions util/uri/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ pub struct Uri<Scheme: UriScheme> {
_scheme: PhantomData<Scheme>,
}

impl<Scheme: UriScheme> Uri<Scheme> {
/// Change the port number of this URI.
pub fn set_port(&mut self, port: u16) {
self.url
.set_port(Some(port))
.expect("should never fail on valid url");
self.port = port;
}
}

impl<Scheme: UriScheme> ConnectionUri for Uri<Scheme> {
fn url(&self) -> &Url {
&self.url
Expand Down

0 comments on commit 0ac1957

Please sign in to comment.