PhoenixMicro.RPC (PhoenixMicro v1.0.0)

Copy Markdown View Source

Request-response RPC over any configured transport.

Each RPC call:

  1. Generates a unique correlation_id.
  2. Publishes the request to service_topic with a reply_to inbox topic.
  3. Subscribes temporarily to the inbox topic.
  4. Waits for a response matching the correlation ID.
  5. Returns {:ok, response_payload} or {:error, reason} on timeout.

Example

# Caller side
{:ok, result} = PhoenixMicro.RPC.call("math.sum", [1, 2, 3], timeout: 3_000)

# Responder side — in a Consumer
defmodule MathConsumer do
  use PhoenixMicro.Consumer
  topic "math.sum"

  def handle(%{payload: numbers, reply_to: reply_to, correlation_id: cid}, _ctx) do
    result = Enum.sum(numbers)
    PhoenixMicro.RPC.respond(reply_to, result, cid)
    :ok
  end
end

Summary

Functions

Performs an RPC call to topic with payload.

Returns a specification to start this module under a supervisor.

Sends a response back to an RPC caller. Should be called from a consumer's handle/2 when message.reply_to is set.

Functions

call(topic, payload, opts \\ [])

@spec call(String.t(), term(), keyword()) ::
  {:ok, term()} | {:error, :timeout} | {:error, term()}

Performs an RPC call to topic with payload.

Options:

  • :timeout (integer, default 5000) — milliseconds to wait for response.
  • :retry (integer, default 0) — number of times to retry on timeout.
  • :transport (atom) — override the transport for this call.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

respond(reply_to, result, correlation_id)

@spec respond(String.t(), term(), String.t()) :: :ok

Sends a response back to an RPC caller. Should be called from a consumer's handle/2 when message.reply_to is set.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()