Skip to content

Commit 74c57b7

Browse files
committed
Address review comments
1 parent 1881f28 commit 74c57b7

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/main/java/com/google/firebase/internal/ApacheHttp2AsyncEntityProducer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
3131
import org.apache.hc.core5.http.nio.DataStreamChannel;
3232

33-
@SuppressWarnings("deprecation")
3433
public class ApacheHttp2AsyncEntityProducer implements AsyncEntityProducer {
3534
private ByteBuffer bytebuf;
3635
private ByteArrayOutputStream baos;
@@ -106,8 +105,8 @@ public void produce(DataStreamChannel channel) throws IOException {
106105
try {
107106
content.writeTo(baos);
108107
} catch (IOException e) {
109-
writeFuture.completeExceptionally(e);
110-
// failed(e);
108+
failed(e);
109+
throw e;
111110
}
112111
}
113112

@@ -139,11 +138,13 @@ public final Exception getException() {
139138

140139
@Override
141140
public void releaseResources() {
142-
bytebuf.clear();
141+
if (bytebuf != null) {
142+
bytebuf.clear();
143+
}
143144
}
144145

145146
@VisibleForTesting
146147
ByteBuffer getBytebuf() {
147148
return bytebuf;
148149
}
149-
}
150+
}

src/main/java/com/google/firebase/internal/ApacheHttp2Request.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.TimeoutException;
3030

3131
import org.apache.hc.client5.http.ConnectTimeoutException;
32+
import org.apache.hc.client5.http.HttpHostConnectException;
3233
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
3334
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
3435
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
@@ -123,10 +124,12 @@ public void cancelled() {
123124
} catch (ExecutionException e) {
124125
if (e.getCause() instanceof ConnectTimeoutException) {
125126
throw new IOException("Connection Timeout", e.getCause());
127+
} else if (e.getCause() instanceof HttpHostConnectException) {
128+
throw new IOException("Connection exception in request", e.getCause());
126129
} else if (e.getCause() instanceof H2StreamResetException) {
127130
throw new IOException("Stream exception in request", e.getCause());
128131
} else {
129-
throw new IOException("Exception in request", e);
132+
throw new IOException("Unknown exception in request", e);
130133
}
131134
} catch (InterruptedException e) {
132135
throw new IOException("Request Interrupted", e);

src/main/java/com/google/firebase/internal/ApacheHttp2Transport.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.apache.hc.client5.http.async.HttpAsyncClient;
2626
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
27+
import org.apache.hc.client5.http.config.ConnectionConfig;
2728
import org.apache.hc.client5.http.config.TlsConfig;
2829
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2930
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
@@ -33,8 +34,10 @@
3334
import org.apache.hc.core5.http2.HttpVersionPolicy;
3435
import org.apache.hc.core5.http2.config.H2Config;
3536
import org.apache.hc.core5.io.CloseMode;
36-
import org.apache.hc.core5.util.TimeValue;
3737

38+
/**
39+
* HTTP/2 enabled async transport based on the Apache HTTP Client library
40+
*/
3841
public final class ApacheHttp2Transport extends HttpTransport {
3942

4043
private final CloseableHttpAsyncClient httpAsyncClient;
@@ -62,14 +65,19 @@ public static CloseableHttpAsyncClient newDefaultHttpAsyncClient() {
6265
public static HttpAsyncClientBuilder defaultHttpAsyncClientBuilder() {
6366
PoolingAsyncClientConnectionManager connectionManager =
6467
new PoolingAsyncClientConnectionManager();
65-
connectionManager.setMaxTotal(100);
66-
connectionManager.setDefaultMaxPerRoute(100);
67-
connectionManager.closeIdle(TimeValue.of(30, TimeUnit.SECONDS));
68+
69+
// Set Max total connections and max per route to match google api client limits
70+
// https://github.com/googleapis/google-http-java-client/blob/f9d4e15bd3c784b1fd3b0f3468000a91c6f79715/google-http-client-apache-v5/src/main/java/com/google/api/client/http/apache/v5/Apache5HttpTransport.java#L151
71+
connectionManager.setMaxTotal(200);
72+
connectionManager.setDefaultMaxPerRoute(20);
73+
connectionManager.setDefaultConnectionConfig(
74+
ConnectionConfig.custom().setTimeToLive(-1, TimeUnit.MILLISECONDS).build());
6875
connectionManager.setDefaultTlsConfig(
6976
TlsConfig.custom().setVersionPolicy(HttpVersionPolicy.NEGOTIATE).build());
7077

7178
return HttpAsyncClientBuilder.create()
72-
.setH2Config(H2Config.DEFAULT)
79+
// Set maxConcurrentStreams to 100 to match the concurrent stream limit of the FCM backend.
80+
.setH2Config(H2Config.custom().setMaxConcurrentStreams(100).build())
7381
.setHttp1Config(Http1Config.DEFAULT)
7482
.setConnectionManager(connectionManager)
7583
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
@@ -88,15 +96,21 @@ protected ApacheHttp2Request buildRequest(String method, String url) {
8896
return new ApacheHttp2Request(httpAsyncClient, requestBuilder);
8997
}
9098

99+
/**
100+
* Gracefully shuts down the connection manager and releases allocated resources. This closes all
101+
* connections, whether they are currently used or not.
102+
*/
91103
@Override
92104
public void shutdown() throws IOException {
93105
httpAsyncClient.close(CloseMode.GRACEFUL);
94106
}
95107

108+
/** Returns the Apache HTTP client. */
96109
public HttpAsyncClient getHttpClient() {
97110
return httpAsyncClient;
98111
}
99112

113+
/** Returns if the underlying HTTP client is mTLS. */
100114
@Override
101115
public boolean isMtls() {
102116
return isMtls;

src/test/java/com/google/firebase/internal/ApacheHttp2TransportIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public class ApacheHttp2TransportIT {
5858
private static final ImmutableMap<String, Object> payload =
5959
ImmutableMap.<String, Object>of("foo", "bar");
6060

61-
// Sets a 5 second delay before response
61+
// Sets a 5 second delay before server response to simulate a slow network that results in a read timeout.
6262
private static final String DELAY_URL = "https://nghttp2.org/httpbin/delay/5";
63-
private static final String NO_CONNECT_URL = "http://google.com:81";
63+
// Points to a unused port that simulates a slow conncetion which results in a conncetion timeout
64+
private static final String NO_CONNECT_URL = "https://google.com:81";
6465
private static final String GET_URL = "https://nghttp2.org/httpbin/get";
6566
private static final String POST_URL = "https://nghttp2.org/httpbin/post";
6667

0 commit comments

Comments
 (0)