Freddy.RPC.Request (freddy v0.17.2)
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.
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
id()
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.
meta()
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.
options()
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.
payload()
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.
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
duration(request, granularity \\ :millisecond)
Specs
duration(t(), granularity :: System.time_unit()) :: integer()
Get duration of the finished request with the given granularity.
get_meta(request, key, default \\ nil)
Specs
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_option(request, option, default \\ nil)
Specs
Get value of the existing option
of the request. If option is not set,
the default
value is returned.
get_timeout(req)
Specs
Get timeout value of the request.
put_meta(request, key, value)
Specs
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.
put_option(request, option, value)
Specs
Add or change existing option
of the request.
remove_meta(request, key)
Specs
Remove the existing key
from the request meta-information. If the key wasn't set,
this action won't have any effect.
remove_option(req, option)
Specs
Removes the existing option
from the request. If the option wasn't set,
this action won't have any effect.
set_payload(request, new_payload)
Specs
Change request payload to new_payload
.
set_routing_key(request, new_routing_key)
Specs
set_routing_key(t(), routing_key()) :: t()
Change request routing key to new_routing_key
.
set_timeout(req, timeout)
Specs
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_payload(request, fun)
Specs
Update request payload with the given function.
update_routing_key(request, fun)
Specs
update_routing_key(t(), (routing_key() -> routing_key())) :: t()
Update request routing key with the given function.