manhvu

manhvu

Intro

EasyRpc for easy to wrap a remote function (rpc) to local module for convenience. Dev just to need add config for target nodes (or use {Module, :function, args} for get nodes in runtime) and add functions want to wrap then use EasyRpc with option for EasyRpc can read the config. Then call rpc in app like a local function.

This library help developer easy to wrap a remote procedure call (rpc, library uses Erlang :erpc module).

EasyRpc supports some basic features for wrapping rpc: retry, timeout, error_handling. Each function can has seperated options or use global options (config for all function in a module).

Source code is available on Github and package on Hex

Example

Add Configs

This way you can add config to config.exs/dev/prod.exs or runtime.exs(for runtime).

config :simple_example, :remote_defrpc,
  nodes: [:"remote@127.0.0.1"],  # or {ClusterHelper, :get_nodes, [:remote_api]},
  select_mode: :round_robin

Declare functions

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

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

Now call rpc like a local function

Remote.get_data()

Thanks for reading!

https://github.com/ohhi-vn/easy_rpc

Showing Posts 1 to 8

hauleth

hauleth

Why I need to define remote functions as part of compile-time config instead of defining them, well, in module? With similar approach as I could do with defdelegate for example.

manhvu

manhvu OP

Simple & easy for newbies is thing for this libray and lib also help reduce boilerplate code & can use with our other module is ClusterHelper (like a global registry for cluster). Remember with with dynamic Elixir cluster much harder than static cluster.

Thanks for you comment!

hauleth

hauleth

Yeah, but I find it easier to reason if instead of writing:

# config/config.ex
config :app_name, :wrapper_name,
  nodes: [:"test1@test.local"], # or using function like nodes: {Module, Fun, Args}
  error_handling: true,
  select_mode: :random,
  module: TargetApp.Interface.Api,
  functions: [
    # {function_name, arity, options}
    {:get_data, 1},
    {:put_data, 1, error_handling: false},
    {:clear, 2, new_name: :clear_data, retry: 3},
    {:clear_all, 0, new_name: :clear_all, private: true}, # wrap to private function.
  ]

# Code
defmodule DataHelper do
  use EasyRpc.RpcWrapper,
    otp_app: :app_name,
    config_name: :account_wrapper

  def process_remote() do
    # call rpc like a local function.
    case get_data("key") do
      {:ok, data} ->
        # do something with data

      {:error, reason} ->
        # handle error
    end
  end
end

# Or call from other module like
{:ok, result} = DataHelper.get_data("my_key")

You would do:

# config/runtime.exs
# Bonus - it supports config to be more runtime thingy
config :app_name, DataHelper,
  nodes: [:"test1@test.local"], # or using function like nodes: {Module, Fun, Args}
  select_mode: :random

# Code
defmodule DataHelper do
  use EasyRpc.RpcWrapper,
    otp_app: :app_name,
    module: TargetApp.Interface.Api,
    error_handling: true

  defrpc get_data(key)
  defrpc put_data(key, data), error_handling: false
  defrpc clear(key, opts), as: :clear_data, retries: 3
  defprcp clear_all(), 

  def process_remote() do
    # call rpc like a local function.
    case get_data("key") do
      {:ok, data} ->
        # do something with data

      {:error, reason} ->
        # handle error
    end
  end
end

# Or call from other module like
{:ok, result} = DataHelper.get_data("my_key")
D4no0

D4no0

Totally agree with your version being more readable. I am in general not a fan of using the tuple format in code if it can be avoided, as it’s very hard to track down.

hauleth

hauleth

In this case it also provides a way to write documentation for these generated functions, which is not possible with original approach.

manhvu

manhvu OP

Yes, I think this way is better than config way. I will add support this in the next version. Thank you for your suggestion!

manhvu

manhvu OP

I have added new way to wrapping rpc. More friendly than last one but still not good enough. I’ finding a way to improve defrpc macro. I hope I can back and improve this soon!

Add Configs

config :simple_example, :remote_defrpc,
  nodes: [:"remote@127.0.0.1"],  # or {ClusterHelper, :get_nodes, [:remote_api]},
  select_mode: :round_robin

Declare functions

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

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

manhvu OP

After some updates, the library now has clearer docs and a better overall design.

It currently supports two ways to wrap RPC:

  1. Config-based approach – Best suited for code generation.
  2. Declarative approach – Easier to read and maintain.

Note: This library works nicely with ClusterHelper, making it easier to build distributed systems in Elixir.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews