Rafael Rocha
Rafael Rocha

Reputation: 31

Concurrent requests with Vert.x

Consider the following Vert.x (version 3.5.3) code.

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    Handler<HttpServerRequest> handler = req -> {
        System.out.println(req.path() + " - start");
        vertx.executeBlocking(f -> {
            sleep(5000);
            f.complete();
        }, false, ar -> {
            req.response().end();
            System.out.println(req.path() + " - end");
        });
    };
    vertx.createHttpServer().requestHandler(handler).listen(8080);
}

To test the server, I run the following javascript code in the browser's console.

for (let i = 1; i < 10; i++) {
    fetch('http://localhost:8080/test' + i).then(data => console.log('test' + i));
}

These requests result in the following server side output.

    /test1 - start
    /test2 - start
    /test3 - start
    /test4 - start
    /test5 - start
    /test6 - start
    >>>>>5 seconds later<<<<<
    /test1 - end
    /test2 - end
    /test7 - start
    /test3 - end
    /test8 - start
    /test4 - end
    /test5 - end
    /test6 - end
    /test9 - start
    >>>>>5 seconds later<<<<<
    /test7 - end
    /test8 - end
    /test9 - end

The ">>>>>5 seconds later<<<<<" lines are shown only to emphasize pauses in the output. It seems only 6 requests are concurrently processed at a time, despite the fact that I'm using executeBlocking with "ordered=false". I was expecting to see all 9 requests starting in the output and, five seconds later, all ending.

Why this happens? Can this behaviour be changed? Am I doing something wrong?

Upvotes: 0

Views: 590

Answers (1)

tsegismont
tsegismont

Reputation: 9138

Browsers open a limited number of connections to a single destination. In your case, this limit seems to be 6.

See Max parallel http connections in a browser?

Upvotes: 2

Related Questions