Freddy.RPC.Request (freddy v0.16.0)

RPC Request data structure. Applications may modify this data structure. For example, one might want to add some specific publication option to every request, or change routing key, based on request payload, or just add some application-specific meta-information.

Examples

Partial implementation of Freddy.RPC.Client behaviour:

alias Freddy.RPC.Request

def before_request(request, state) do
  new_request =
    request
    |> Request.put_option(:priority, 9)
    |> Request.put_option(:user_id, Application.get_env(:my_app, :rabbitmq)[:user_id])
    |> Request.update_payload(fn %{} = payload -> Map.put(payload, :now, DateTime.utc_now())

  {:ok, new_request, state}
end

def on_response(response, request, state) do
  Logger.info("Request #{request.id} is completed in #{Request.duration(request)} ms")

  {:reply, response, state}
end

Link to this section Summary

Types

Request identifier, unique for every request.

Request meta information.

Request options.

Request payload.

Request routing key.

t()

Functions

Get duration of the finished request with the given granularity.

Get a value with the given key from the request meta-information dictionary. If the key is not set, the default value will be returned.

Get value of the existing option of the request. If option is not set, the default value is returned.

Get timeout value of the request.

Add arbitrary meta information with given key and value to the request. This information is not used by Freddy itself, but can be used by customized clients or servers to carry some additional information throughout the request lifecycle.

Add or change existing option of the request.

Remove the existing key from the request meta-information. If the key wasn't set, this action won't have any effect.

Removes the existing option from the request. If the option wasn't set, this action won't have any effect.

Change request payload to new_payload.

Change request routing key to new_routing_key.

Change request timeout value. The timeout value can be given as a positive integer, or an :infinity atom. This value will be used by the RPC client to notify requester, that the server hasn't responded within a given timeout milliseconds interval.

Update request payload with the given function.

Update request routing key with the given function.

Link to this section Types

Specs

id() :: String.t()

Request identifier, unique for every request.

Applications SHOULD NOT change the generated identifier, but MAY use its value, for example, for logging purposes.

Specs

meta() :: map()

Request meta information.

Applications MAY read and modify meta-information using functions put_meta/3, get_meta/3 and remove_meta/2. Applications SHOULD NOT modify this field directly.

Specs

options() :: Keyword.t()

Request options.

Applications MAY read and modify request options using functions put_option/3, get_option/3 and remove_option/2. Applications SHOULD NOT modify this field directly.

See Freddy.Publisher.publish/4 for available options.

Specs

payload() :: term()

Request payload.

Can be of any type initially. Applications MUST encode this field to a string (binary) before sending the message to server.

Link to this type

routing_key()

Specs

routing_key() :: String.t()

Request routing key.

Applications MAY modify request routing key using functions set_routing_key/2 and update_routing_key/2.

Specs

t() :: %Freddy.RPC.Request{
  from: from(),
  id: id(),
  meta: map(),
  options: options(),
  payload: payload(),
  routing_key: routing_key(),
  start_time: time(),
  stop_time: time() | nil,
  timer: timer()
}

Link to this section Functions

Link to this function

duration(request, granularity \\ :millisecond)

Specs

duration(t(), granularity :: System.time_unit()) :: integer()

Get duration of the finished request with the given granularity.

Link to this function

get_meta(request, key, default \\ nil)

Specs

get_meta(t(), key :: term(), default :: term()) :: term()

Get a value with the given key from the request meta-information dictionary. If the key is not set, the default value will be returned.

Link to this function

get_option(request, option, default \\ nil)

Specs

get_option(t(), option :: atom(), default :: term()) :: term()

Get value of the existing option of the request. If option is not set, the default value is returned.

Link to this function

get_timeout(req)

Specs

get_timeout(t()) :: timeout()

Get timeout value of the request.

Link to this function

put_meta(request, key, value)

Specs

put_meta(t(), key :: term(), value :: term()) :: t()

Add arbitrary meta information with given key and value to the request. This information is not used by Freddy itself, but can be used by customized clients or servers to carry some additional information throughout the request lifecycle.

Link to this function

put_option(request, option, value)

Specs

put_option(t(), option :: atom(), value :: term()) :: t()

Add or change existing option of the request.

Link to this function

remove_meta(request, key)

Specs

remove_meta(t(), key :: term()) :: t()

Remove the existing key from the request meta-information. If the key wasn't set, this action won't have any effect.

Link to this function

remove_option(req, option)

Specs

remove_option(t(), option :: atom()) :: t()

Removes the existing option from the request. If the option wasn't set, this action won't have any effect.

Link to this function

set_payload(request, new_payload)

Specs

set_payload(t(), payload()) :: t()

Change request payload to new_payload.

Link to this function

set_routing_key(request, new_routing_key)

Specs

set_routing_key(t(), routing_key()) :: t()

Change request routing key to new_routing_key.

Link to this function

set_timeout(req, timeout)

Specs

set_timeout(t(), timeout()) :: t()

Change request timeout value. The timeout value can be given as a positive integer, or an :infinity atom. This value will be used by the RPC client to notify requester, that the server hasn't responded within a given timeout milliseconds interval.

Link to this function

update_payload(request, fun)

Specs

update_payload(t(), (payload() -> payload())) :: t()

Update request payload with the given function.

Link to this function

update_routing_key(request, fun)

Specs

update_routing_key(t(), (routing_key() -> routing_key())) :: t()

Update request routing key with the given function.