Skip to content

Commit 21482ac

Browse files
committed
Experiment with HttpClient HTTP request/response API method design that provides non racy methods when called outside event-loop.
1 parent d2fcda6 commit 21482ac

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/main/java/io/vertx/core/http/HttpClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.vertx.core.net.SSLOptions;
2020

2121
import java.util.concurrent.TimeUnit;
22+
import java.util.function.Function;
2223

2324
/**
2425
* An asynchronous HTTP client.
@@ -59,6 +60,10 @@ public interface HttpClient extends io.vertx.core.metrics.Measured {
5960
*/
6061
Future<HttpClientRequest> request(RequestOptions options);
6162

63+
default <T> Future<T> request(RequestOptions options, Function<HttpClientRequest, Future<T>> handler) {
64+
return request(options).compose(handler);
65+
}
66+
6267
/**
6368
* Create an HTTP request to send to the server at the {@code host} and {@code port}.
6469
*
@@ -72,6 +77,10 @@ default Future<HttpClientRequest> request(HttpMethod method, int port, String ho
7277
return request(new RequestOptions().setMethod(method).setPort(port).setHost(host).setURI(requestURI));
7378
}
7479

80+
default <T> Future<T> request(HttpMethod method, int port, String host, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
81+
return request(method, port, host, requestURI).compose(handler);
82+
}
83+
7584
/**
7685
* Create an HTTP request to send to the server at the {@code host} and default port.
7786
*
@@ -84,6 +93,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String host, String
8493
return request(new RequestOptions().setMethod(method).setHost(host).setURI(requestURI));
8594
}
8695

96+
default <T> Future<T> request(HttpMethod method, String host, String requestURI , Function<HttpClientRequest, Future<T>> handler) {
97+
return request(method, host, requestURI).compose(handler);
98+
}
99+
87100
/**
88101
* Create an HTTP request to send to the server at the default host and port.
89102
*
@@ -95,6 +108,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String requestURI)
95108
return request(new RequestOptions().setMethod(method).setURI(requestURI));
96109
}
97110

111+
default <T> Future<T> request(HttpMethod method, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
112+
return request(method, requestURI).compose(handler);
113+
}
114+
98115
/**
99116
* Close the client immediately ({@code close(0, TimeUnit.SECONDS}).
100117
*

src/test/java/io/vertx/core/http/HttpTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6544,10 +6544,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
65446544
}
65456545
);
65466546
startServer(testAddress);
6547-
client.request(requestOptions)
6548-
.compose(req -> req.send()
6547+
client.request(requestOptions, req -> req
6548+
.send()
65496549
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
6550-
.compose(HttpClientResponse::end))
6550+
.compose(HttpClientResponse::end)
6551+
)
65516552
.onComplete(onSuccess(nothing -> complete()));
65526553
await();
65536554
}

0 commit comments

Comments
 (0)