hare v0.1.9 Hare.RPC.Server behaviour

A behaviour module for implementing AMQP RPC server processes.

The Hare.RPC.Server module provides a way to create processes that hold, monitor, and restart a channel in case of failure, and have some callbacks to hook into the process lifecycle and handle messages.

An example Hare.RPC.Server process that responds messages with "ping" as payload with a "pong" response, otherwise it does not ack but calls a given handler function with the payload and a callback function, so the handler can respond when the message is processed:

defmodule MyRPC.Server do
  use Hare.RPC.Server

  def start_link(conn, config, handler) do
    Hare.RPC.Server.start_link(__MODULE__, conn, config, handler)
  end

  def init(handler) do
    {:ok, %{handler: handler}}
  end

  def handle_request("ping", _meta, state) do
    {:reply, "pong", state}
  end
  def handle_request(payload, meta, %{handler: handler} = state) do
    callback = &Hare.RPC.Server.reply(meta, &1)

    handler.(payload, callback)
    {:noreply, state}
  end
end

Channel handling

When the Hare.RPC.Server starts with start_link/5 it runs the init/1 callback and responds with {:ok, pid} on success, like a GenServer.

After starting the process it attempts to open a channel on the given connection. It monitors the channel, and in case of failure it tries to reopen again and again on the same connection.

Context setup

The context setup process for a RPC server is to declare an exchange, then declare a queue to consume, and then bind the queue to the exchange. It also creates a default exchange to use it to respond to the reply-to queue.

Every time a channel is open the context is set up, meaning that the queue and the exchange are declared and binded through the new channel based on the given configuration.

The configuration must be a Keyword.t that contains the following keys:

Summary

Functions

Responds a request given its meta

Starts a Hare.RPC.Server process linked to the current process

Callbacks

Called every time the channel has been opened and the queue, exchange, and binding has been declared

Called when the process receives a message

Called when the AMQP server has registered the process as a RPC server and it will start to receive requests

Called when a request is received from the queue

Called when the RPC server process is first started. start_link/5 will block until it returns

This callback is the same as the GenServer equivalent and is called when the process terminates. The first argument is the reason the process is about to exit with

Types

meta()
meta() :: map
payload()
payload() :: Hare.Adapter.payload
state()
state() :: term

Functions

reply(meta, response)
reply(meta, response :: binary) :: :ok

Responds a request given its meta

start_link(mod, conn, config, initial, opts \\ [])
start_link(module, pid, config, initial :: term, GenServer.options) :: GenServer.on_start

Starts a Hare.RPC.Server process linked to the current process.

This function is used to start a Hare.Consumer process in a supervision tree. The process will be started by calling init with the given initial value.

Arguments:

  • mod - the module that defines the server callbacks (like GenServer)
  • conn - the pid of a Hare.Core.Conn process
  • config - the configuration of the publisher (describing the exchange to declare)
  • initial - the value that will be given to init/1
  • opts - the GenServer options

Callbacks

connected(meta, state)
connected(meta, state) ::
  {:noreply, state} |
  {:stop, reason :: term, state}

Called every time the channel has been opened and the queue, exchange, and binding has been declared.

It is called with two arguments: some metadata and the process’ internal state.

The metadata is a map with a two fields:

Returning {:noreply, state} will cause the process to enter the main loop with state as its internal state.

Returning {:stop, reason, state} will terminate the loop and call terminate(reason, state) before the process exists with reason reason.

handle_info(meta, state)
handle_info(meta, state) ::
  {:noreply, state} |
  {:stop, reason :: term, state}

Called when the process receives a message.

Returning {:noreply, state} will causes the process to enter the main loop with the given state.

Returning {:stop, reason, state} will not send the message, terminate the main loop and call terminate(reason, state) before the process exists with reason reason.

handle_ready(meta, state)
handle_ready(meta, state) ::
  {:noreply, state} |
  {:stop, reason :: term, state}

Called when the AMQP server has registered the process as a RPC server and it will start to receive requests.

Returning {:noreply, state} will causes the process to enter the main loop with the given state.

Returning {:stop, reason, state} will terminate the main loop and call terminate(reason, state) before the process exists with reason reason.

handle_request(payload, meta, state)
handle_request(payload, meta, state) ::
  {:noreply, state} |
  {:reply, response :: binary, state} |
  {:stop, reason :: term, state}

Called when a request is received from the queue.

The arguments are the message’s payload, some metadata and the internal state. The metadata is a map containing all metadata given by the adapter when receiving the message plus the :exchange and :queue values received at the connect/2 callback.

Returning {:reply, response, state} will respond inmediately to the client and enter the main loop with the given state.

Returning {:noreply, state} will enter the main loop with the given state without responding. Therefore, Hare.RPC.Server.reply/2 should be used to respond to the client.

Returning {:stop, reason, state} will terminate the main loop and call terminate(reason, state) before the process exists with reason reason.

init(initial)
init(initial :: term) :: GenServer.on_start

Called when the RPC server process is first started. start_link/5 will block until it returns.

It receives as argument the fourth argument given to start_link/5.

Returning {:ok, state} will cause start_link/5 to return {:ok, pid} and attempt to open a channel on the given connection and declare the queue, the exchange, and the binding. After that it will enter the main loop with state as its internal state.

Returning :ignore will cause start_link/5 to return :ignore and the process will exit normally without entering the loop, opening a channel or calling terminate/2.

Returning {:stop, reason} will cause start_link/5 to return {:error, reason} and the process will exit with reason reason without entering the loop, opening a channel, or calling terminate/2.

terminate(reason, state)
terminate(reason :: term, state) :: any

This callback is the same as the GenServer equivalent and is called when the process terminates. The first argument is the reason the process is about to exit with.