Reactor

Reactor is a unit that contains NIO selector, single selector's thread and scheduler executor.

Usage

The same single instance of reactor could be shared across multiple crushers. To utilize more CPU you can create separate instances of NioReactor for each crusher.


DatagramCrusherBuilder.builder()
    .withReactor(reactor)
    // ...
    .buildAndOpen();

Closing

NioReactor implements java.io.Closeable and must be explicitly closed after usage:


NioReactor reactor = new NioReactor();

// ... operate

reactor.close();

or even more shorter usage:


try (NioReactor reactor = new NioReactor()) {

    // ... operate

}

Tick duration

NioReactor can be constructed with "tick" duration parameters that specifies the sleep period of the selector's thread. The lower values of tick make throttling more precise, the larger values of tick save CPU usage. The default value of tick is 20 milliseconds.


// Tick value is 5 millisecond
NioReactor reactor = new NioReactor(5);

Scheduler

NioReactor's scheduler can be used for scheduling delayed crusher operation.


reactor.getScheduler().scheduleFreeze(crusher, 3000, TimeUnit.MILLISECONDS);

Or any arbitrary operation.


reactor.getScheduler().schedule(() -> {
        crusher.unfreezeAllPairs();
        return true;
    }, 3000, TimeUnit.MILLISECONDS);

Listeners

Also all listener will be called with scheduler executor thread in deferred mode (is on by default).


TcpCrusherBuilder.builder()
    // ...
    .withDeferredListeners(true)
    .withCreationListener((addr) ->
        LOGGER.info("Client is created <{}>", addr))
    .withDeletionListener((addr, byteMeters) ->
        LOGGER.info("Client is deleted <{}>", addr))
    // ...
    buildAndOpen();

When deferred mode is off all listener calls will be made right within selector thread so there should be no any long blocking operation in listener code.