EasyRpc.DefRpc (EasyRpc v0.4.2)

View Source

This is second way to add a rpc to local module. In this way, you need declare all rpc functions in a module. Configs for nodes (list of node or {Module, :function, args}) can put to config.exs for runtime.exs file.

GUIDE

Add config for DefRpc

Add config to config file in your project.

config :simple_example, :remote_defrpc,
  nodes: [:"remote@127.0.0.1"],  # or using {module, function, args} like: {ClusterHelper, :get_nodes, [:remote_api]},
  select_mode: :round_robin # default is :random
  sticky_node: true # default is false

Explain config:

:nodes List of nodes, or {module, function, args} on local node.

:select_mode Select node mode, default is :random. Support for [:random, :round_robin, :hash]. :round_robin will use per process to select node. :hash will using :erlang.phash2 to select node in list, data is arguments of function.

:sticky_node If set to true, the node will be sticky for each call. Default is false.

Note: sticky_node & round_robin are supported for rpc per process only.

Using DefRpc in your module

Add your global config for function & config info.

  use EasyRpc.DefRpc,
    otp_app: :simple_example,
    config_name: :remote_defrpc,
    # Remote module name
    module: RemoteNode.Interface,
    timeout: 1000,
    retry: 0

Explain:

:otp_app Application name for config.

:config_name Config name for rpc.

:module Remote module name.

:timeout Timeout for rpc, default for all functions. If set timeout for function, it will override this value.

:retry Retry times for rpc, default for all functions. If set retry for function, it will override this value.

:error_handling Enable error handling (catch all) or not. In case of retry > 0, error_handling will be always true.

Define rpc functions

defrpc :get_data
defrpc :put_data, args: 1
defrpc :clear, args: 2, as: :clear_data, private: true
defrpc :put_data, args: [:name], new_name: :put_with_retry, retry: 3, timeout: 1000

Explain:

:args Number of arguments for function. If set to 0 or missed, function will be defined without arguments.

:as New name for function. If set, function will be defined with new name.

:private If set to true, the function will be defined as private function. Default is false (public function).

:retry Retry times for rpc, default for all functions. If set retry for function, it will override this value.

:timeout Timeout for rpc, default for all functions. If set timeout for function, it will override this value.

:error_handling Enable error handling (catch all) or not. In case of retry > 0, error_handling will be always true.

Summary

Functions

defrpc(fun, fun_opts \\ [])

(macro)