Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient HTTP request/response API method design that provides non racy methods when called outside event-loop #4705

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions vertx-core/src/main/java/io/vertx/core/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.core.Future;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
* The API to interacts with an HTTP server.
Expand All @@ -41,6 +42,10 @@ default Future<HttpClientRequest> request() {
*/
Future<HttpClientRequest> request(RequestOptions options);

default <T> Future<T> request(RequestOptions options, Function<HttpClientRequest, Future<T>> handler) {
return request(options).compose(handler);
}

/**
* Create an HTTP request to send to the server at the {@code host} and {@code port}.
*
Expand All @@ -54,6 +59,10 @@ default Future<HttpClientRequest> request(HttpMethod method, int port, String ho
return request(new RequestOptions().setMethod(method).setPort(port).setHost(host).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, int port, String host, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
return request(method, port, host, requestURI).compose(handler);
}

/**
* Create an HTTP request to send to the server at the {@code host} and default port.
*
Expand All @@ -66,6 +75,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String host, String
return request(new RequestOptions().setMethod(method).setHost(host).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, String host, String requestURI , Function<HttpClientRequest, Future<T>> handler) {
return request(method, host, requestURI).compose(handler);
}

/**
* Create an HTTP request to send to the server at the default host and port.
*
Expand All @@ -77,6 +90,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String requestURI)
return request(new RequestOptions().setMethod(method).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
return request(method, requestURI).compose(handler);
}

/**
* Shutdown with a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}).
*
Expand Down
7 changes: 4 additions & 3 deletions vertx-core/src/test/java/io/vertx/tests/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6813,10 +6813,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
}
);
startServer(testAddress);
client.request(requestOptions)
.compose(req -> req.send()
client.request(requestOptions, req -> req
.send()
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::end))
.compose(HttpClientResponse::end)
)
.onComplete(onSuccess(nothing -> complete()));
await();
}
Expand Down
Loading