[undertow-dev] Using UndertowClient from one server to call another server

Stuart Douglas sdouglas at redhat.com
Mon Aug 28 21:50:26 EDT 2017


This looks like a bug in the client with regards to how it handles
HTTP upgrade. If the second request is added before the initial
upgrade request is fully processed you can get this error.

If you file a JIRA I will look into it. For now a possible workaround
would be to use prior knowledge rather than upgrade (by using a URL of
the form h2c-prior://target:p8080/whatever).

Stuart

On Tue, Aug 29, 2017 at 11:25 AM, Steve Hu <stevehu at gmail.com> wrote:
> Hi,
>
> I am trying to use UndertowClient to call another service in the current
> service handleRequest using HTTP 2.0. If I have concurrent requests to the
> current service, then I will get the following error.
>
> java.io.IOException: UT001033: Invalid connection state
> at
> io.undertow.client.http.HttpClientConnection.sendRequest(HttpClientConnection.java:336)
>
> Since HTTP 2.0 connection is multiplex, so I use instance variables for
> UndertowClient instance and ClientConnection instance and don't close the
> connection for each call.
>
> My question is how many requests can go through the same connection? Is the
> Invalid connection state caused by too many requests in the same connection?
> If yes, I can use a connection pool just like the HTTP 1.1. Also, is it
> possible that the server will close the connection if it is idle for a
> period of time?
>
> Here is the handler code for reference.
>
> public class DataGetHandler implements HttpHandler {
>     UndertowClient client = UndertowClient.getInstance();
>     ClientConnection connection;
>
>     @Override
>     public void handleRequest(HttpServerExchange exchange) throws Exception
> {
>         List<String> list = new ArrayList<>();
>         final CountDownLatch latch = new CountDownLatch(1);
>         if(connection == null) {
>             try {
>                 connection = client.connect(new URI(apidHost),
> Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ?
> OptionMap.create(UndertowOptions.ENABLE_HTTP2, true):
> OptionMap.EMPTY).get();
>             } catch (Exception e) {
>                 logger.error("Exeption:", e);
>                 throw new ClientException(e);
>             }
>         }
>         final AtomicReference<ClientResponse> reference = new
> AtomicReference<>();
>         try {
>             ClientRequest request = new
> ClientRequest().setMethod(Methods.GET).setPath(apidPath);
>             connection.sendRequest(request,
> client.createClientCallback(reference, latch));
>             latch.await();
>             int statusCode = reference.get().getResponseCode();
>             if(statusCode >= 300){
>                 throw new Exception("Failed to call API D: " + statusCode);
>             }
>             List<String> apidList =
> Config.getInstance().getMapper().readValue(reference.get().getAttachment(Http2Client.RESPONSE_BODY),
>                     new TypeReference<List<String>>(){});
>             list.addAll(apidList);
>         } catch (Exception e) {
>             logger.error("Exception:", e);
>             throw new ClientException(e);
>         }
>         list.add("API C: Message 1");
>         list.add("API C: Message 2");
>
> exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(list));
>     }
> }
>
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev


More information about the undertow-dev mailing list