Skip to content

Event Handler Runners

Petar Marinov edited this page Jun 24, 2018 · 2 revisions

IEventHandlerRunner implementation is responsible for running the event handlers for when corresponding event is published.
Usually event handlers are executed on background threads.

Implementations

  • FixedCountThreadsRunner - uses a given number of background threads to execute the event handlers on. The threads are created when in the constructor and released when disposed.
// will use 3 threads for executing event handlers
var runner = new FixedCountThreadsRunner(3);
  • DynamicCountThreadsRunner - creates and releases background threads based on provided configuration.
var runner = new DynamicCountThreadsRunnerConfig(
  checkInterval: TimeSpan.FromMilliseconds(100),
  waitingHandlersCountTreshold: 2,
  cyclesBeforeAddingThread: 2,
  cyclesBeforeReleasingThread: 20,
  maxThreadsCount: 5);

In the given example the runner will check every 100 milliseconds how many event handlers are waiting; if there are more than 2 for 2 cycles (2x100=200 milliseconds) a new thread will be created; if there are less than 2 event handlers waiting for 20 cycles (20x100=2000 milliseconds) a thread will be released; there will be no more than 5 threads.
Parameters configuration can cover different scenarios e.g. low waitingHandlersCountTreshold and high cyclesBeforeReleasingThread will result in quick creation of threads and keeping them longer.

  • UnrestrictedThreadPoolRunner - executes every event handler on a ThreadPool thread. May cause a thread starvation on high load scenarios.

  • RestrictedThreadPoolRunner - executes every event handler on a ThreadPool thread, restricting the number of event handlers running concurrently.

// will have maximum 5 handlers running concurrently
var runner = new RestrictedThreadPoolRunner(5);
  • CallerThreadBlockingRunner - executes event handlers on the thread of the publisher, blocking it until all handlers finish execution.
Clone this wiki locally