Skip to content

Instantly share code, notes, and snippets.

@oli-h
Created April 20, 2018 10:17
Show Gist options
  • Save oli-h/fe5607132509c51a57d130f821271189 to your computer and use it in GitHub Desktop.
Save oli-h/fe5607132509c51a57d130f821271189 to your computer and use it in GitHub Desktop.
package ch.oli.vertx;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServer;
public class ReproducerTcpClose {
private static final Vertx VERTX = Vertx.vertx();
public static void main(String[] args) throws Exception {
startBackendServer(); // listen on Port 7020
HttpClientOptions clOpts = new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(true);
final HttpClient httpClient = VERTX.createHttpClient(clOpts);
for (int i = 1; ; i++) {
String uri = "id-" + i;
HttpClientRequest req = httpClient.get(7020, "localhost", uri);
req.handler(resp -> {
int status = resp.statusCode();
if (status != 200) {
System.out.println("Request " + uri + ": Non-200-Status " + status);
}
resp.handler(data -> {
// System.out.println("Request #" + uri + ": received " + data.length() + " bytes");
});
resp.endHandler(end -> {
System.out.println("Request " + uri + ": finished (response complete)");
});
});
req.connectionHandler(conn -> {
System.out.println("Connection established for " + uri);
});
req.end();
System.out.println("\n\nRequest " + uri + ": starting");
Thread.sleep(5000);
}
}
private static void startBackendServer() {
final HttpServer httpServer = VERTX.createHttpServer();
httpServer.requestHandler(req -> {
System.out.println("-------------------------- Backend-Server received request " + req.uri());
VERTX.setTimer(500, id -> {
req.response().end("my data");
System.out.println("-------------------------- Backend-Server finished response " + req.uri());
});
});
httpServer.listen(7020);
}
}
@oli-h
Copy link
Author

oli-h commented Apr 20, 2018

With vertx-3.5.0 we see

Request id-1: starting
Connection established for id-1
-------------------------- Backend-Server received request id-1
-------------------------- Backend-Server finished response id-1
Request id-1: finished (response complete)

Request id-2: starting
-------------------------- Backend-Server received request id-2
-------------------------- Backend-Server finished response id-2
Request id-2: finished (response complete)

Request id-3: starting
-------------------------- Backend-Server received request id-3
-------------------------- Backend-Server finished response id-3
Request id-3: finished (response complete)

With vertx-3.5.1 we see:

Request id-1: starting
Connection established for id-1
-------------------------- Backend-Server received request id-1
-------------------------- Backend-Server finished response id-1
Request id-1: finished (response complete)

Request id-2: starting
Connection established for id-2
-------------------------- Backend-Server received request id-2
-------------------------- Backend-Server finished response id-2
Request id-2: finished (response complete)

Request id-3: starting
Connection established for id-3
-------------------------- Backend-Server received request id-3
-------------------------- Backend-Server finished response id-3
Request id-3: finished (response complete)

Note the three Connection established for ... - a new TCP-connection is established for every single http cycle

@oli-h
Copy link
Author

oli-h commented Apr 20, 2018

Note: vertx-core master branch (3.6.0-SNAPSHOT) again behaves like vertx-3.5.0 - so it really makes use of "keep-alive" connections between two http-requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment