Search code examples
vert.xvertx-verticlevert.x-webclient

What happens to vertx.eventloop thread once the control passes to blockingHandler?


I am using vert.x as api gateway and each request has to go through multiple handlers Sample code snippet

router.route(BASE_PATH)
                .method(HttpMethod.POST)
                .handler(LoggerHandler.create(LoggerFormat.SHORT))
                .handler(BodyHandler.create())
                .blockingHandler(this::authRouter)
                .blockingHandler(this::reqValidationRouter)
                .handler(this::downStreamRouter)
                .blockingHandler(this::responseTransformRouter)

What happens to event loop threads when the control passes to blockingHandler? Do they continue to accept more requests? If yes, what happens when the blocking handler execution completes? Does this switching from eventLoop to blockingHandler (workerPool) and then back to eventLoop has any performance implications?

What is the ideal way to handle multiple handlers?

Thanks, Nitish Goyal


Solution

  • What happens to event loop threads when the control passes to blockingHandler? Do they continue to accept more requests?

    Yes, the event loop will offload the blocking handler part to the worker pool and handle other events.

    If yes, what happens when the blocking handler execution completes?

    An event with the result is added to the event loop queue.

    Does this switching from eventLoop to blockingHandler (workerPool) and then back to eventLoop has any performance implications?

    Switching between threads is not free but the cost should be negligible in the overall latency for such a use case (api gateway).

    What is the ideal way to handle multiple handlers?

    Ideally you'd avoid blocking code in your Vert.x Web handlers.