0

I'm using Vertx 3.5.0 and very new to it. I'm trying to cancel the code execution when a client cancels their request.

Currently it's setup to where the first thing we do is deploy a verticle to run an HttpServer, and we add all of our Routes to the Router. From here we have a handler function per route. Inside this handler I'm trying this:

routingContext.request().connection().closeHandler({
//execute logic for ending execution
});

This is the only method I've seen that actually catches the closing of the connection, but the problem is it doesn't execute the handler early enough in the eventloop. So if I have any logs in there it will look like:

...[vert.x-eventloop-thread-0].....

...[vert.x-eventloop-thread-0]..... (Let's say I cancelled the request at this point)

...[vert.x-eventloop-thread-0].....

...[vert.x-eventloop-thread-0]..... (Final log of regular execution before waiting on asynchronous db calls)

...[vert.x-eventloop-thread-0]..... (Execution of closeHandler code)

I'd like for the closeHandler code to interrupt the process and execute essentially when the event actually happens.

This seems to always be the case regardless of when I cancel the request so I figure I'm missing something about how Vertx is handling the asynchronousity.

I've tried executing the closeHandler code via a worker verticle, inside the blockingHandler from the Router object, and inside the connectionHandler from the HttpServer object. All had the same result.

The main code execution is also not executed by a worker verticle, just a regular one.

Thanks!

1 Answers1

0

It seems you misunderstand what a closeHandler is. It's a callback method, that Vert.x invokes when the request is being closed. It is not way to terminate the request early.

If you would like to terminate request early, one way is to use response().close() instead.

As a footnote, I'd like to mention that Vert.x 3.5.0 is 4 years old now, and you should be upgrading to 3.9, or, if you can, to 4.0

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • Ah I should've worded it better; I'm trying to set up a handler for when a client cancels their request prior to receiving the full response, not close the connection myself. As for the version, I agree! But my company is a stickler for upgrades sadly. – guy_who_can't_code Mar 28 '21 at 22:45