EasyRpc.Pipeline (EasyRpc v1.1.0)

Copy Markdown View Source

Builds and executes a middleware pipeline for RPC calls.

A pipeline is an ordered list of {plug_module, opts} tuples. Plugs are composed inside-out — the first plug in the list is the outermost middleware and the last plug is the innermost.

Default pipeline

[Retry, Logger, NodeSelector, Executor, ErrorHandler]

This replicates the behaviour of the original RpcCall.

Summary

Functions

Composes a pipeline into a single (Context.t() -> Context.t()) function.

Executes a pipeline by composing the plugs into a chain and calling it with the given context.

Default pipeline that mirrors the original RpcCall behaviour.

Runs the default pipeline and returns the final context.

Extracts the final result from a pipeline context.

Convenience: creates a context, runs the pipeline, and extracts the result.

Types

plug_entry()

@type plug_entry() :: {module(), term()}

t()

@type t() :: [plug_entry()]

Functions

build_chain(pipeline)

@spec build_chain(t()) :: (EasyRpc.Context.t() -> EasyRpc.Context.t())

Composes a pipeline into a single (Context.t() -> Context.t()) function.

Plugs are folded right-to-left so that pipeline.first becomes the outermost middleware.

call(pipeline, ctx)

@spec call(t(), EasyRpc.Context.t()) :: EasyRpc.Context.t()

Executes a pipeline by composing the plugs into a chain and calling it with the given context.

The returned context contains the final state — inspect ctx.result, ctx.error, and ctx.halted after calling.

default()

@spec default() :: t()

Default pipeline that mirrors the original RpcCall behaviour.

Order (outermost → innermost):

  1. ErrorHandler — normalises result/error on the context
  2. Retry — wraps the entire call, re-executes on failure
  3. NodeSelector — picks a target node
  4. Logger — logs before and after each attempt
  5. Executor — calls :erpc.call

execute(ctx)

@spec execute(EasyRpc.Context.t()) :: EasyRpc.Context.t()

Runs the default pipeline and returns the final context.

Uses the pre-composed default chain — prefer this over call/2 when running the default pipeline, since no composition happens per call.

extract_result(ctx, config)

@spec extract_result(EasyRpc.Context.t(), EasyRpc.WrapperConfig.t()) :: term()

Extracts the final result from a pipeline context.

Handles both bare mode (raises on error) and safe mode (returns {:ok, result} | {:error, error}).

In safe mode, results already normalised by ErrorHandler (tagged tuples) pass through untouched; anything else is wrapped in {:ok, _}.

run(config, function, args, pipeline \\ nil)

@spec run(EasyRpc.WrapperConfig.t(), atom(), list(), t() | nil) :: term()

Convenience: creates a context, runs the pipeline, and extracts the result.

This is the primary entry point used by code-generated wrapper functions. When pipeline is omitted, the pre-composed default pipeline is used.