View Source ZenMonitor.Local.Dispatcher (ZenMonitor v2.0.1)

ZenMonitor.Local.Dispatcher is a GenStage Consumer responsible for throttled delivery of down messages.

ZenMonitor.Local acts as a GenStage Producer, it stores all of the down messages that need to be dispatched based off of what has been enqueued by the ZenMonitor.Local.Connector.

The Dispatcher will deliver these messages throttled by a maximum rate which is controlled by the {:zen_monitor, :demand_interval} and {:zen_monitor, :demand_amount} settings.

To calculate the maximum number of messages processed per second you can use the following formula:

maximum_mps = (demand_amount) * (1000 / demand_interval)

For example, if the demand_amount is 1000, and demand_interval is 100 (milliseconds) the maximum messages per second are:

maximum_mps = (1000) * (1000 / 100)

       -> (1000) * 10
       -> 10_000

For convenience a ZenMonitor.Local.Dispatcher.maximum_mps/0 is provided that will perform this calculation.

Link to this section Summary

Functions

Gets the demand amount from the Application Environment

Puts the demand amount into the Application Environment

Gets the demand interval from the Application Environment

Puts the demand interval into the Application Environment

Handles the events for dispatch

Handles the periodic generate_demand message

Handles the callback for the subscription being established with the producer.

Callback implementation for GenStage.init/1.

Calculate the current maximum messages per second

Link to this section Functions

Specs

demand_amount() :: integer()

Gets the demand amount from the Application Environment

The demand amount is the number of events tor request from the GenStage Producer (ZenMonitor.Local) every demand interval

This can be controlled at boot and runtime with the {:zen_monitor, :demand_amount} setting, see ZenMonitor.Local.Dispatcher.demand_amount/1 for runtime convenience functionality.

Specs

demand_amount(value :: integer()) :: :ok

Puts the demand amount into the Application Environment

This is a simple convenience function for overwriting the {:zen_monitor, :demand_amount} setting at runtime.

Specs

demand_interval() :: integer()

Gets the demand interval from the Application Environment

The demand interval is the number of milliseconds to wait between demanding more events from the GenStage Producer (ZenMonitor.Local)

This can be controlled at boot and runtime with the {:zen_monitor, :demand_interval} setting, see ZenMonitor.Local.Dispatcher.demand_interval/1 for runtime convenience functionality.

Specs

demand_interval(value :: integer()) :: :ok

Puts the demand interval into the Application Environment

This is a simple convenience function for overwrite the {:zen_monitor, :demand_interval} setting at runtime

Link to this function

handle_events(events, from, producer)

View Source

Handles the events for dispatch

Dispatch is a simple two step procedure followed for each message to be dispatched.

  1. Check if the message is still valid. Messages can become invalid if the monitor was demonitored after the message was enqueued.

2a. If valid: forward the message to the subscriber 2b. If invalid: skip message

Event dispatch will calculate an "unfulfilled" demand based off the number of messages skipped and demand that the producer provide additional events so that MPS is maintained and prevent the Dispatcher from being starved because of invalid messages.

Link to this function

handle_info(atom, producer)

View Source

Handles the periodic generate_demand message

Asks the producer for demand_amount of events then schedules the next demand generation.

Link to this function

handle_subscribe(atom, _, from, state)

View Source

Handles the callback for the subscription being established with the producer.

This is the start of the demand loop, once the producer confirms subscription, the initial call to schedule_demand/0 happens.

Callback implementation for GenStage.init/1.

Specs

maximum_mps() :: float()

Calculate the current maximum messages per second

This is a convenience function to help operators understand the current throughput of the Dispatcher.