24
24
25
25
import org .apache .hc .client5 .http .async .HttpAsyncClient ;
26
26
import org .apache .hc .client5 .http .async .methods .SimpleRequestBuilder ;
27
+ import org .apache .hc .client5 .http .config .ConnectionConfig ;
27
28
import org .apache .hc .client5 .http .config .TlsConfig ;
28
29
import org .apache .hc .client5 .http .impl .async .CloseableHttpAsyncClient ;
29
30
import org .apache .hc .client5 .http .impl .async .HttpAsyncClientBuilder ;
33
34
import org .apache .hc .core5 .http2 .HttpVersionPolicy ;
34
35
import org .apache .hc .core5 .http2 .config .H2Config ;
35
36
import org .apache .hc .core5 .io .CloseMode ;
36
- import org .apache .hc .core5 .util .TimeValue ;
37
37
38
+ /**
39
+ * HTTP/2 enabled async transport based on the Apache HTTP Client library
40
+ */
38
41
public final class ApacheHttp2Transport extends HttpTransport {
39
42
40
43
private final CloseableHttpAsyncClient httpAsyncClient ;
@@ -62,14 +65,19 @@ public static CloseableHttpAsyncClient newDefaultHttpAsyncClient() {
62
65
public static HttpAsyncClientBuilder defaultHttpAsyncClientBuilder () {
63
66
PoolingAsyncClientConnectionManager connectionManager =
64
67
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 ());
68
75
connectionManager .setDefaultTlsConfig (
69
76
TlsConfig .custom ().setVersionPolicy (HttpVersionPolicy .NEGOTIATE ).build ());
70
77
71
78
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 ())
73
81
.setHttp1Config (Http1Config .DEFAULT )
74
82
.setConnectionManager (connectionManager )
75
83
.setRoutePlanner (new SystemDefaultRoutePlanner (ProxySelector .getDefault ()))
@@ -88,15 +96,21 @@ protected ApacheHttp2Request buildRequest(String method, String url) {
88
96
return new ApacheHttp2Request (httpAsyncClient , requestBuilder );
89
97
}
90
98
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
+ */
91
103
@ Override
92
104
public void shutdown () throws IOException {
93
105
httpAsyncClient .close (CloseMode .GRACEFUL );
94
106
}
95
107
108
+ /** Returns the Apache HTTP client. */
96
109
public HttpAsyncClient getHttpClient () {
97
110
return httpAsyncClient ;
98
111
}
99
112
113
+ /** Returns if the underlying HTTP client is mTLS. */
100
114
@ Override
101
115
public boolean isMtls () {
102
116
return isMtls ;
0 commit comments