Skip to content

Commit 087acf4

Browse files
committed
restore initial config
1 parent 8ca94a4 commit 087acf4

4 files changed

Lines changed: 98 additions & 74 deletions

File tree

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ApacheHttpRequest.java

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.api.client.http.LowLevelHttpRequest;
1818
import com.google.api.client.http.LowLevelHttpResponse;
1919

20+
import java.io.ByteArrayOutputStream;
2021
import java.io.IOException;
2122
import java.util.concurrent.CompletableFuture;
2223
import java.util.concurrent.ExecutionException;
@@ -27,6 +28,7 @@
2728
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
2829
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
2930
import org.apache.hc.client5.http.config.RequestConfig;
31+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
3032
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
3133
import org.apache.hc.core5.concurrent.FutureCallback;
3234
import org.apache.hc.core5.http.ContentType;
@@ -42,73 +44,77 @@
4244
import org.apache.hc.core5.util.Timeout;
4345
import org.apache.http.HttpEntity;
4446

45-
/** @author Yaniv Inbar */
47+
/**
48+
* @author Yaniv Inbar
49+
*/
4650
final class ApacheHttpRequest extends LowLevelHttpRequest {
47-
private final HttpAsyncClientBuilder httpClientBuilder;
48-
private final SimpleHttpRequest request;
51+
private final HttpAsyncClientBuilder httpClientBuilder;
52+
private final SimpleHttpRequest request;
4953

50-
private RequestConfig.Builder requestConfig;
54+
private RequestConfig.Builder requestConfig;
5155

52-
ApacheHttpRequest(HttpAsyncClientBuilder httpClientBuilder, SimpleHttpRequest request) {
53-
this.httpClientBuilder = httpClientBuilder;
54-
this.request = request;
55-
// disable redirects as google-http-client handles redirects
56-
this.requestConfig =
57-
RequestConfig.custom()
58-
.setRedirectsEnabled(false)
59-
// TODO: enable set these somewhere down the call
56+
ApacheHttpRequest(HttpAsyncClientBuilder httpClientBuilder, SimpleHttpRequest request) {
57+
this.httpClientBuilder = httpClientBuilder;
58+
this.request = request;
59+
// disable redirects as google-http-client handles redirects
60+
this.requestConfig =
61+
RequestConfig.custom()
62+
.setRedirectsEnabled(false)
63+
// TODO: enable set these somewhere down the call
6064
// .setNormalizeUri(false)
6165
// .setStaleConnectionCheckEnabled(false)
62-
;
63-
}
66+
;
67+
}
6468

65-
@Override
66-
public void addHeader(String name, String value) {
67-
request.addHeader(name, value);
68-
}
69+
@Override
70+
public void addHeader(String name, String value) {
71+
request.addHeader(name, value);
72+
}
6973

70-
@Override
71-
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
72-
IOReactorConfig newConfig = IOReactorConfig.custom()
73-
.setSoTimeout(Timeout.ofMilliseconds(readTimeout))
74-
.build();
75-
requestConfig.setConnectTimeout(Timeout.ofMilliseconds(connectTimeout));
76-
httpClientBuilder.setIOReactorConfig(newConfig);
77-
}
74+
@Override
75+
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
76+
IOReactorConfig newConfig = IOReactorConfig.custom()
77+
.setSoTimeout(Timeout.ofMilliseconds(readTimeout))
78+
.build();
79+
requestConfig.setConnectTimeout(Timeout.ofMilliseconds(connectTimeout));
80+
httpClientBuilder.setIOReactorConfig(newConfig);
81+
}
7882

79-
@Override
80-
public LowLevelHttpResponse execute() throws IOException {
83+
@Override
84+
public LowLevelHttpResponse execute() throws IOException {
8185
ApacheHttpRequestEntityProducer entityProducer = new ApacheHttpRequestEntityProducer(this);
8286

83-
request.setConfig(requestConfig.build());
84-
final CompletableFuture<SimpleHttpResponse> responseFuture = new CompletableFuture<>();
85-
try {
86-
httpClientBuilder.build().execute(
87+
request.setConfig(requestConfig.build());
88+
final CompletableFuture<SimpleHttpResponse> responseFuture = new CompletableFuture<>();
89+
try {
90+
CloseableHttpAsyncClient client = httpClientBuilder.build();
91+
client.start();
92+
client.execute(
8793
new BasicRequestProducer(request, entityProducer),
88-
SimpleResponseConsumer.create(),
89-
new FutureCallback<SimpleHttpResponse>() {
90-
@Override
91-
public void completed(final SimpleHttpResponse response) {
92-
responseFuture.complete(response);
93-
}
94+
SimpleResponseConsumer.create(),
95+
new FutureCallback<SimpleHttpResponse>() {
96+
@Override
97+
public void completed(final SimpleHttpResponse response) {
98+
responseFuture.complete(response);
99+
}
94100

95-
@Override
96-
public void failed(final Exception exception) {
97-
responseFuture.completeExceptionally(exception);
98-
}
101+
@Override
102+
public void failed(final Exception exception) {
103+
responseFuture.completeExceptionally(exception);
104+
}
99105

100-
@Override
101-
public void cancelled() {
102-
responseFuture.cancel(false);
103-
}
104-
}
105-
);
106-
final SimpleHttpResponse response = responseFuture.get();
107-
return new ApacheHttpResponse(request, response);
108-
} catch (InterruptedException | ExecutionException e) {
109-
e.printStackTrace();
110-
throw new IOException("Error making request", e);
106+
@Override
107+
public void cancelled() {
108+
responseFuture.cancel(false);
109+
}
110+
}
111+
);
112+
final SimpleHttpResponse response = responseFuture.get();
113+
return new ApacheHttpResponse(request, response);
114+
} catch (InterruptedException | ExecutionException e) {
115+
e.printStackTrace();
116+
throw new IOException("Error making request", e);
117+
}
111118
}
112-
}
113119

114120
}

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ApacheHttpRequestEntity.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ public int available() {
6767

6868
@Override
6969
public void produce(DataStreamChannel dataStreamChannel) throws IOException {
70-
if (request.getStreamingContent() == null) {
71-
return;
72-
}
7370
OutputStream dataStreamOutputStream = new OutputStream() {
74-
// diegomarquezp: the buffer size may have effects in performance - I just chose an arbitrary one
75-
private final java.nio.ByteBuffer buffer = ByteBuffer.allocate(100 * 1000);
71+
private final java.nio.ByteBuffer buffer = ByteBuffer.allocate(1000);
7672

7773
@Override
7874
public void write(int b) throws IOException {
@@ -96,6 +92,7 @@ public void close() throws IOException {
9692
}
9793
};
9894
request.getStreamingContent().writeTo(dataStreamOutputStream);
95+
dataStreamOutputStream.close();
9996
}
10097

10198
@Override

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ApacheHttpTransport.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
import com.google.api.client.http.HttpTransport;
1818
import com.google.api.client.util.Beta;
1919
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
20-
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
20+
import org.apache.hc.client5.http.config.TlsConfig;
2121
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2222
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
2323
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
2424
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
2525
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner;
2626
import org.apache.hc.client5.http.config.ConnectionConfig;
27-
import org.apache.hc.core5.http.HttpRequest;
27+
import org.apache.hc.core5.http.config.Http1Config;
28+
import org.apache.hc.core5.http2.HttpVersionPolicy;
29+
import org.apache.hc.core5.http2.config.H2Config;
30+
import org.apache.hc.core5.reactor.IOReactorConfig;
2831

2932
import java.io.IOException;
3033
import java.net.ProxySelector;
@@ -52,7 +55,7 @@ public final class ApacheHttpTransport extends HttpTransport {
5255
/**
5356
* Apache HTTP client.
5457
*/
55-
private final HttpAsyncClientBuilder httpClient;
58+
private final HttpAsyncClientBuilder httpClientBuilder;
5659

5760
/**
5861
* If the HTTP client uses mTLS channel.
@@ -82,11 +85,11 @@ public ApacheHttpTransport() {
8285
* <li>Retries are disabled (google-http-client handles retries).
8386
* </ul>
8487
*
85-
* @param httpClient Apache HTTP client to use
88+
* @param httpClientBuilder Apache HTTP client to use
8689
* @since 1.30
8790
*/
88-
public ApacheHttpTransport(HttpAsyncClientBuilder httpClient) {
89-
this.httpClient = httpClient;
91+
public ApacheHttpTransport(HttpAsyncClientBuilder httpClientBuilder) {
92+
this.httpClientBuilder = httpClientBuilder;
9093
this.isMtls = false;
9194
}
9295

@@ -105,13 +108,13 @@ public ApacheHttpTransport(HttpAsyncClientBuilder httpClient) {
105108
* <li>Retries are disabled (google-http-client handles retries).
106109
* </ul>
107110
*
108-
* @param httpClient Apache HTTP client to use
111+
* @param httpClientBuilder Apache HTTP client to use
109112
* @param isMtls If the HTTP client is mutual TLS
110113
* @since 1.38
111114
*/
112115
@Beta
113-
public ApacheHttpTransport(HttpAsyncClientBuilder httpClient, boolean isMtls) {
114-
this.httpClient = httpClient;
116+
public ApacheHttpTransport(HttpAsyncClientBuilder httpClientBuilder, boolean isMtls) {
117+
this.httpClientBuilder = httpClientBuilder;
115118
this.isMtls = isMtls;
116119
}
117120

@@ -166,9 +169,13 @@ public static HttpAsyncClientBuilder newDefaultHttpClientBuilder() {
166169
connectionManager.setMaxTotal(200);
167170
connectionManager.setDefaultMaxPerRoute(20);
168171
connectionManager.setDefaultConnectionConfig(connectionConfig);
172+
connectionManager.setDefaultTlsConfig(TlsConfig.custom().setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2).build());
173+
169174

170175
return HttpAsyncClientBuilder.create()
171-
.useSystemProperties()
176+
.setH2Config(H2Config.DEFAULT)
177+
.setHttp1Config(Http1Config.DEFAULT)
178+
.setIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(20).build())
172179
.setConnectionManager(connectionManager)
173180
// socket factories are not configurable in the async client
174181
//.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
@@ -185,7 +192,7 @@ public boolean supportsMethod(String method) {
185192
@Override
186193
protected ApacheHttpRequest buildRequest(String method, String url) {
187194
SimpleHttpRequest request = new SimpleHttpRequest(method, URI.create(url));
188-
return new ApacheHttpRequest(httpClient, request);
195+
return new ApacheHttpRequest(httpClientBuilder, request);
189196
}
190197

191198
/**
@@ -205,7 +212,7 @@ public void shutdown() throws IOException {
205212
* @since 1.30
206213
*/
207214
public HttpAsyncClientBuilder getHttpClientBuilder() {
208-
return httpClient;
215+
return httpClientBuilder;
209216
}
210217

211218
/**

google-http-client-apache-v3/src/test/java/com/google/api/client/http/apache/v3/FirebaseTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,24 @@
2020
import java.util.concurrent.TimeUnit;
2121

2222
public class FirebaseTest {
23-
public static FirebaseApp setup_admin_apache_http2() throws IOException {
23+
public static FirebaseApp setup_admin_apache_http2_v3() throws IOException {
2424
FileInputStream serviceAccount = new FileInputStream("src/main/resources/cert.json");
2525

2626
FirebaseOptions options = FirebaseOptions.builder()
2727
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
28+
.setProjectId("spring-autoconf-test")
29+
.setHttpTransport(new ApacheHttp2Transport(true))
30+
.build();
31+
32+
return FirebaseApp.initializeApp(options);
33+
}
34+
35+
public static FirebaseApp setup_admin_apache_http_v3() throws IOException {
36+
FileInputStream serviceAccount = new FileInputStream("src/main/resources/cert.json");
37+
38+
FirebaseOptions options = FirebaseOptions.builder()
39+
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
40+
.setProjectId("spring-autoconf-test")
2841
.setHttpTransport(new ApacheHttpTransport())
2942
.build();
3043

@@ -118,10 +131,11 @@ public void testFireBase() throws FirebaseMessagingException, IOException, Inter
118131
FirebaseApp app;
119132

120133
System.out.println("Start");
121-
app = setup_admin_apache_http2();
134+
app = setup_admin_apache_http2_v3();
135+
// app = setup_admin_apache_http_v3();
122136
benchmark_send_each(messages, numRequests, app);
123137
benchmark_send_each_async(messages, numRequests, app);
124-
benchmark_send_each(messages, numRequests, app);
138+
benchmark_send_all(messages, numRequests, app);
125139

126140
System.out.println("Sleep");
127141
TimeUnit.SECONDS.sleep(5);

0 commit comments

Comments
 (0)