Skip to content

Commit

Permalink
Merge pull request #151 from GoogleCloudPlatform/endpoint-url
Browse files Browse the repository at this point in the history
fix: allow specifying protocol in the endpoint
  • Loading branch information
nimf committed Oct 25, 2022
2 parents 232470e + 07db1c0 commit f47d674
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.grpc.MethodDescriptor;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -213,6 +215,27 @@ private GcpManagedChannelOptions prepareGcpManagedChannelConfig(
.build();
}

private ManagedChannelBuilder<?> channelBuilderForEndpoint(String endpoint) {
String serviceAddress;
// Assume https by default.
int port = 443;
try {
URL url = new URL(endpoint);
serviceAddress = url.getHost();
port = url.getPort() < 0 ? url.getDefaultPort() : url.getPort();
} catch (MalformedURLException ex) {
// When no protocol is specified, fallback to plain host:port parsing.
int colon = endpoint.lastIndexOf(':');
if (colon < 0) {
serviceAddress = endpoint;
} else {
serviceAddress = endpoint.substring(0, colon);
port = Integer.parseInt(endpoint.substring(colon + 1));
}
}
return ManagedChannelBuilder.forAddress(serviceAddress, port);
}

/**
* Update the list of MultiEndpoint configurations.
*
Expand Down Expand Up @@ -270,18 +293,7 @@ public synchronized void setMultiEndpoints(List<GcpMultiEndpointOptions> meOptio
if (options.getChannelCredentials() != null) {
managedChannelBuilder = Grpc.newChannelBuilder(e, options.getChannelCredentials());
} else {
String serviceAddress;
int port;
int colon = e.lastIndexOf(':');
if (colon < 0) {
serviceAddress = e;
// Assume https by default.
port = 443;
} else {
serviceAddress = e.substring(0, colon);
port = Integer.parseInt(e.substring(colon + 1));
}
managedChannelBuilder = ManagedChannelBuilder.forAddress(serviceAddress, port);
managedChannelBuilder = channelBuilderForEndpoint(e);
}
if (options.getChannelConfigurator() != null) {
managedChannelBuilder = options.getChannelConfigurator().apply(managedChannelBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ public void testSpannerMultiEndpointClient() throws IOException, InterruptedExce
// Follower-first multi-endpoint endpoints.
final List<String> followerEndpoints = new ArrayList<>();
final String leaderEndpoint = "us-east4.googleapis.com:443";
final String followerEndpoint = "us-east1.googleapis.com:443";
// Test endpoint with protocol specified.
final String followerEndpoint = "https://us-east1.googleapis.com:443";
leaderEndpoints.add(leaderEndpoint);
leaderEndpoints.add(followerEndpoint);
followerEndpoints.add(followerEndpoint);
Expand Down Expand Up @@ -677,8 +678,8 @@ public <ReqT, RespT> ApiCallContext configure(ApiCallContext context, ReqT reque
contextFor.apply("follower").run(readQuery);
assertThat(getOkCallsCount(fakeRegistry, followerEndpoint)).isEqualTo(1);

// Replace leader endpoints.
final String newLeaderEndpoint = "us-west1.googleapis.com:443";
// Replace leader endpoints. Try endpoint with default port.
final String newLeaderEndpoint = "https://us-west1.googleapis.com";
leaderEndpoints.clear();
leaderEndpoints.add(newLeaderEndpoint);
leaderEndpoints.add(followerEndpoint);
Expand Down

0 comments on commit f47d674

Please sign in to comment.