Skip to content

Commit 435dd8c

Browse files
authored
fix: Handle null server address (#12184)
In case of an invalid endpoint such as `localhost:-1`, the current parsing logic returns null for server address. This PR made server address nullable. And added a null check for the port parsing. In addition, added a try catch for the whole parsing logic because serviceAddress and serverPort should only be used for obersevability and should not affect regular requests.
1 parent a7c92e0 commit 435dd8c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public static EndpointContext getDefaultInstance() {
134134

135135
public abstract String resolvedEndpoint();
136136

137+
@Nullable
137138
public abstract String resolvedServerAddress();
138139

139140
@Nullable
@@ -410,7 +411,7 @@ private Integer parseServerPort(String endpoint) {
410411
return null;
411412
}
412413
HostAndPort hostAndPort = parseServerHostAndPort(endpoint);
413-
if (!hostAndPort.hasPort()) {
414+
if (hostAndPort == null || !hostAndPort.hasPort()) {
414415
return null;
415416
}
416417
return hostAndPort.getPort();
@@ -466,8 +467,13 @@ public EndpointContext build() throws IOException {
466467
setResolvedUniverseDomain(determineUniverseDomain());
467468
String endpoint = determineEndpoint();
468469
setResolvedEndpoint(endpoint);
469-
setResolvedServerAddress(parseServerAddress(resolvedEndpoint()));
470-
setResolvedServerPort(parseServerPort(resolvedEndpoint()));
470+
try {
471+
setResolvedServerAddress(parseServerAddress(resolvedEndpoint()));
472+
setResolvedServerPort(parseServerPort(resolvedEndpoint()));
473+
} catch (Exception throwable) {
474+
// Server address and server port are only used for observability.
475+
// We should ignore any errors parsing them and not affect the main client requests.
476+
}
471477
setUseS2A(shouldUseS2A());
472478
return autoBuild();
473479
}

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,4 +683,19 @@ void endpointContextBuild_resolvesPort() throws IOException {
683683
.build();
684684
Truth.assertThat(endpointContext.resolvedServerPort()).isNull();
685685
}
686+
687+
@Test
688+
void endpointContextBuild_resolvesInvalidEndpointAndPort() throws Exception {
689+
690+
String endpoint = "localhost:-1";
691+
692+
EndpointContext endpointContext =
693+
defaultEndpointContextBuilder
694+
.setClientSettingsEndpoint(endpoint)
695+
.setTransportChannelProviderEndpoint(null)
696+
.build();
697+
698+
Truth.assertThat(endpointContext.resolvedServerAddress()).isNull();
699+
Truth.assertThat(endpointContext.resolvedServerPort()).isNull();
700+
}
686701
}

0 commit comments

Comments
 (0)