Last active
February 14, 2018 15:55
-
-
Save oli-h/ed8df718311a8480eb24adc0d5ceb408 to your computer and use it in GitHub Desktop.
Reproducer for https://github.com/eclipse/vert.x/issues/2065
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.vertx.core.Vertx; | |
import io.vertx.core.http.*; | |
import io.vertx.core.streams.Pump; | |
public class ReproducePausedStreamInPool { | |
public static void main(String[] args) throws Exception { | |
final Vertx vertx = Vertx.vertx(); | |
startBackendServer(vertx); // listen on Port 7020 | |
startProxy(vertx); // listen on Port 7013, forwards to 7020 | |
HttpClientOptions clOpts = new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(true); | |
final HttpClient httpClient = vertx.createHttpClient(clOpts); | |
for (int i = 1; ; i++) { | |
String uri = "id-" + i; | |
// String uri = "/houston/services/beeble/index.html?" + i; | |
HttpClientRequest req = httpClient.get(7013, "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 #" + id + ": received " + data.length() + " bytes"); | |
// resp.pause(); | |
// vertx.setTimer(10, timeout -> { | |
// resp.resume(); | |
// }); | |
}); | |
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("Request " + uri + ": starting"); | |
// close every 10th request before http-response arrives - this provokes the "paused" stream in Houston | |
if (i % 2 == 0) { | |
while (true) { | |
// need to wait as the connection is only available sometime in near future... | |
final HttpConnection connection = req.connection(); | |
if (connection != null) { | |
System.out.println("closed " + uri); | |
connection.close(); | |
break; | |
} | |
} | |
} | |
Thread.sleep(2000); | |
} | |
} | |
private static void startBackendServer(Vertx vertx) { | |
final HttpServer httpServer = vertx.createHttpServer(); | |
httpServer.requestHandler(req -> { | |
System.out.println("-------------------------- Backend-Server received request " + req.uri()); | |
req.response().end("my data"); | |
System.out.println("-------------------------- Backend-Server finished response " + req.uri()); | |
}); | |
httpServer.listen(7020); | |
} | |
private static void startProxy(Vertx vertx) { | |
final HttpServer httpServer = vertx.createHttpServer(); | |
final HttpClient httpClient = vertx.createHttpClient(); | |
httpServer.requestHandler(req -> { | |
final String uri = req.uri(); | |
System.out.println("--------------- Proxy received request " + uri); | |
final HttpServerResponse resp = req.response(); | |
final HttpClientRequest cReq = httpClient.request(req.method(), 7020, "localhost", uri, cResp -> { | |
System.out.println("--------------- Proxy receives response " + uri); | |
resp.setChunked(true); | |
resp.setStatusCode(cResp.statusCode()); | |
// ============================================================ | |
// TOGGLE HERE BETWEEN PUMP (which leaves paused channels in httpClient's connection pool) AND NON-PUMP (stable) | |
Pump.pump(cResp, resp).start(); | |
// cResp.handler(resp::write); | |
// ============================================================ | |
cResp.endHandler(end -> { | |
resp.end(); | |
System.out.println("--------------- Proxy finished response " + uri); | |
}); | |
}); | |
cReq.exceptionHandler(ex -> { | |
ex.printStackTrace(); | |
resp.setStatusCode(500); | |
resp.setStatusMessage(ex.getMessage()); | |
resp.end(); | |
}); | |
cReq.setTimeout(1000); | |
cReq.end(); | |
}); | |
httpServer.listen(7013); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
See also PCAP file of this test run: https://github.com/oli-h/gateleen/blob/demonstrate504timeout.pcap/demonstrate504timeout.pcap.pcapng
Open with WIreshark and use filter "tcp.stream eq 0" to "tcp.stream eq 4". The "tcp.stream eq 1" is the stream Proxy->BackendServer which is half paused - however you don't see this on the TCP network layer.