EasyRpc.RpcWrapper (EasyRpc v0.1.5)

View Source

This module provides a wrapper for RPC (Remote Procedure Call) in Elixir. It helps to call remote functions as local functions. The library uses macro to create a local function (declare by config).

Currently, the you can wrapping multiple remote functions in a module. You can use same name or different name for remote functions. You also can have multiple wrappers for multiple remote modules.

Guide

Add config for RpcWrapper

Put config to config.exs file in your project. For multi wrapper, you need to separate configs for each wrapper.

Example:

config :app_name, :wrapper_name,
  nodes: [:"test1@test.local", :"test2@test.local"],
  # or nodes: {MyModule, :get_nodes, []}
  error_handling: true, # enable error handling, global setting for all functions.
  select_mode: :random, # select node mode, global setting for all functions.
  module: TargetApp.RemoteModule,
  functions: [
    # {function_name, arity, options \ []}
    {:get_data, 1},
    {:put_data, 1, error_handling: false},
    {:clear, 2, [new_name: :clear_data]},
    {:put_data, 1, [new_name: :put_with_retry, retry: 3]}
  ]

Explain config:

:nodes List of nodes, or {module, function, args} on local node. :module Module of remote functions on remote node. :error_handling Enable error handling (catch all) or not. :select_mode Select node mode, support for random, round_robin, hash. :functions List of functions, each function is a tuple {function_name, arity} or {function_name, new_name, arity, opts}. :options Keyword of options, including new_name, retry, error_handling. Ex: [new_name: :clear_data, retry: 0, error_handling: true]. If retry is set, the function will retry n times when error occurs and error_handling will be applied. If error_handling is set, the function will catch all exceptions and return {:error, reason}.

RpcWrapper

Usage: by using RpcWrapper in your module, you can call remote functions as local functions.

Example:

defmodule DataHelper do
use EasyRpc.RpcWrapper,
  otp_app: :app_name,
  config_name: :wrapper_name

def process_remote() do
  case get_data("key") do
    {:ok, data} ->
      # do something with data
    {:error, reason} ->
      # handle error
  end
end

Explain: :otp_app, name of application will add config :config_name, name of config in application config.

Summary

Functions

Get config from application env.

Get config from application env.

rpc wrapper, support for define macro.

Validate select strategy.

Functions

get_config!(app_name, config_name)

@spec get_config!(atom(), atom()) :: any()

Get config from application env.

get_config!(app_name, config_name, atom)

@spec get_config!(atom(), atom(), atom()) :: any()

Get config from application env.

rpc_call(arg, args, strategy \\ :random)

@spec rpc_call({list() | tuple(), atom(), atom()}, list(), atom()) :: any()

rpc wrapper, support for define macro.

rpc_call_error_handling(arg, args, strategy \\ :random, retry \\ 0)

rpc wrapper, support for define macro.

valid_strategy!(strategy)

@spec valid_strategy!(atom()) :: any()

Validate select strategy.