ryanhex53
ryanhex53

Reputation: 717

what will happen if vertx setPeriodic delay is little than its handler execution time

In a worker verticle using vertx.setPeriodic to setup a timer, let's say the delay is 100ms but the handler cost 5000ms to finish, what will happen if the app running for a long time. Will it cause a memory leak problem ? If it does, how to void the memory problem ?

vertx.setPeriodic(100, timer -> {
    vertx.executeBlocking(future -> {
        // here are some blocking code may cause 5000ms to complete the future.
    }, res -> {
    });
})

Upvotes: 1

Views: 2586

Answers (1)

Amit Patil
Amit Patil

Reputation: 790

Eventually yes it will cause memory issues because,

Since code inside periodic is executing inside executeBlocking it will be executing on worker pool configured (which has fix size) and Since order parameter is not specified, each executeBlocking call will be executed serially (i.e. one after another).

Since periodic is running on event loop thread, it will never wait and will continue to trigger after fix period passed to it(100 ms in your case).

hence, calls to code inside periodic will be executed serially and will get stacked up. Once worker pool exhausts, calls on executeBlocking will go into the wait state.

Few choices to handle this can be (One of or a combination of),

  1. Have separate Worker verticle for periodic.
  2. Define separate worker pool by name dedicated for periodic.
  3. Fine-tune the periodic frequency as per average time required to run periodic job.
  4. If you don’t care about ordering you can call executeBlocking specifying false as the argument to ordered. In this case, any executeBlocking may be executed in parallel on the worker pool.

Upvotes: 1

Related Questions