poa_backend v0.1.0 POABackend.Receiver behaviour View Source

Defines a Receiver Plugin.

A Receiver plugin will run in an independent process and will run the metrics_received/3 function every time it receives a metric from the agents.

POABackend app reads the Receivers configuration from the config.exs file when bootstrap and will create a process per each one of them. That configuration is referenced by :receivers key.

config :poa_backend,
   :receivers,
   [
     {name, module, args}
   ]

for example

config :poa_backend,
   :receivers,
   [
     {:my_receiver, POABackend.Receivers.MyReceiver, [host: "localhost", port: 1234]}
   ]

name, module and args must be defined in the configuration file.

  • name: Name for the new created process. Must be unique
  • module: Module which implements the Receiver behaviour
  • args: Initial args which will be passed to the init_receiver/1 function

The Receiver’s mechanism is built on top of GenStage. Receivers are Consumers (sinks) and they must be subscribed to one or more Producers. The Producers are the Metric types (i.e. ethereum_metrics) and are defined in the config file too:

config :poa_backend,
       :metrics,
       [
         :ethereum_metrics
       ]

In order to work properly we have to define in the configuration file the relation between the Receiver and the Metric types it wants to receive.

config :poa_backend,
     :subscriptions,
     [
       {receiver_name, [metric_type1, metric_type2]}
     ]

for example

config :poa_backend,
       :subscriptions,
       [
         {:my_receiver, [:ethereum_metrics]}
       ]

Implementing A Receiver Plugin

In order to implement your Receiver Plugin you must implement 3 functions.

  • init_receiver/1: Called only once when the process starts
  • metrics_received/3: This function is called eveytime the Producer (metric type) receives a message.
  • handle_message/1: This is called when the Receiver process receives an Erlang message
  • handle_inactive/2: This function is called when one client has been disconnected or is not active for a period of time.
  • terminate/1: Called just before stopping the process

This is a simple example of custom Receiver Plugin

defmodule POABackend.Receivers.MyReceiver do
  use POABackend.Receiver

  def init_receiver(_args) do
    {:ok, :no_state}
  end

  def metrics_received(metrics, from, state) do
    for metric <- metrics do
      IO.puts "metric received"
    end
    {:ok, state}
  end

  def handle_message(_message, state) do
    {:ok, state}
  end

  def handle_inactive(agent_id, state) do
    {:ok, state}
  end

  def terminate(_state) do
    :ok
  end

end

Link to this section Summary

Callbacks

This function is called when a Custom Handler detects a client is inactive

In this callback is called when the Receiver process receives an erlang message

A callback executed when the Receiver Plugin starts. The argument is retrieved from the configuration file when the Receiver is defined It must return {:ok, state}, that state will be keept as in GenServer and can be retrieved in the metrics_received/3 function

This callback will be called every time a message to the subscribed metric type arrives. It must return the tuple {:ok, state}

This callback is called just before the Process goes down. This is a good place for closing connections

Link to this section Callbacks

Link to this callback handle_inactive(agent_id, state) View Source
handle_inactive(agent_id :: binary(), state :: any()) :: {:ok, state :: any()}

This function is called when a Custom Handler detects a client is inactive.

The Custom Handler must to call explicity to POABackend.CustomHandler.publish_inactive/1 and it will publish the inactive message to all the metrics in the system (defined in the config file).

Link to this callback handle_message(msg, state) View Source
handle_message(msg :: any(), state :: any()) :: {:ok, state :: any()}

In this callback is called when the Receiver process receives an erlang message.

It must return {:ok, state}.

Link to this callback init_receiver(args) View Source
init_receiver(args :: term()) :: {:ok, state :: any()}

A callback executed when the Receiver Plugin starts. The argument is retrieved from the configuration file when the Receiver is defined It must return {:ok, state}, that state will be keept as in GenServer and can be retrieved in the metrics_received/3 function.

Link to this callback metrics_received(metrics, from, state) View Source
metrics_received(metrics :: [term()], from :: pid(), state :: any()) ::
  {:ok, state :: any()}

This callback will be called every time a message to the subscribed metric type arrives. It must return the tuple {:ok, state}

Link to this callback terminate(state) View Source
terminate(state :: term()) :: term()

This callback is called just before the Process goes down. This is a good place for closing connections.