2424
2525import org .apache .hc .client5 .http .async .HttpAsyncClient ;
2626import org .apache .hc .client5 .http .async .methods .SimpleRequestBuilder ;
27+ import org .apache .hc .client5 .http .config .ConnectionConfig ;
2728import org .apache .hc .client5 .http .config .TlsConfig ;
2829import org .apache .hc .client5 .http .impl .async .CloseableHttpAsyncClient ;
2930import org .apache .hc .client5 .http .impl .async .HttpAsyncClientBuilder ;
3334import org .apache .hc .core5 .http2 .HttpVersionPolicy ;
3435import org .apache .hc .core5 .http2 .config .H2Config ;
3536import 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+ */
3841public 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 ;
0 commit comments