victorolinasc

victorolinasc

Hello people!

I am about to write a “SDK” for a service. Before I got started, I was looking for some general patterns and best practices.

One thing I noticed is that most of them don’t worry about multiple “instances”. Almost all of them that I’ve used or have seen are using the single instance approach. By that I mean: they have a config like:

# Configuration
config :my_library, client_id: id, client_key: key

defmodule MyApplication do
  # possibly add MyLibrary to the application supervision
end

# Then on application code
MyLibrary.make_some_call(params)

Examples: :ex_aws, :ex_twilio, :sendgrid, etc…

Ecto uses a different approach that facilitates the multiple instances approach but it makes it a bit more verbose to configure it.

# Configuration
config :my_app, MyRepo1, options

config :my_app, MyRepo2, other_options

defmodule MyRepo1 do
  use Ecto.Repo, opts # where otp_app: :my_app
end

defmodule MyRepo2 do
  use Ecto.Repo, opts # where otp_app: :my_app
end

defmodule MyApplication do
  # add MyRepo1 and MyRepo2 to the application supervision
end

# Then on application code
some_query |> MyRepo1.all()
some_query |> MyRepo2.all()

With the first approach, I don’t think there is a good way to have two instances of the library running at the same time. With the second approach we end up needing some macros…

I would like to hear people’s opinion on this:

  1. Should we, as library authors, strive to always provide a multi-instance configuration?
  2. Are there other ways to do this cleanly? Even if that demands Elixir 1.9+…

Cheers!

Most Liked

keathley

keathley

Generally, libraries shouldn’t rely on application env at all. A lot of libraries continue to use that pattern because it was popular in the early days of Elixir. But its a bad model and shouldn’t be copied. If your library needs to use processes, then users should be able to pass in arguments when they start the process in their supervisor. Redix is an excellent example of how to do this well.

There are exceptions to this rule. Sometimes it makes the most sense to provide a full-blown OTP Application. I typically do this because there’s nothing in the application to configure, the supervision hierarchy is complex, or if there’s no benefit to the user managing the lifecycle of the process.

Building a library that is re-usable without collisions is often much more difficult. But I think it pays off in the long run.

Last Post!

keathley

keathley

Let me see if I can clarify this a bit. If we have this code:

config :my_lib, MyRepo,
  adapter: Sandbox # value is determined at compile time

defmodule MyRepo do
  defp adapter do
    # Even if next line is called during app boot, it uses a compile time value
    Application.get_env(:my_lib, MyRepo)[:adapter]
  end
end

The adapter isn’t actually being “compiled” into the MyRepo module. Its always being fetched from the application env. That means its possible to do something like this:

# The adapter is set from when `config.exs` was initially run
Sandbox = MyRepo.adapter()

# Update the config...
Application.put_env(:my_lib, MyRepo, [adapter: DBAdapter])

# Now the adapter has changed
DBAdapter = MyRepo.adapter()

Its possible to compile the adapter directly into the module doing something like this:

defmodule Repo do
  defmacro __using__(_opts) do
    quote do
      @before_compile unquote(__MODULE__)
    end
  end

  defmacro __before_compile__(env) do
    a = Application.get_env(:my_lib, Repo)[:adapter]

    quote do
      def adapter do
        unquote(a)
      end
    end
  end
end

defmodule MyRepo do
  use Repo
end

This will cause the adapter function to be hard-coded with the adapter module into MyRepo.

Hopefully that provides some clarity :).

Everything else seems spot on to me :+1:.

In my applications and services I’ll often use this pattern:

defmodule Server do
  def start_link(opts) do
    GenServer.start_link(__MODULE__, state, name: opts[:name] || __MODULE__)
  end

  def foo(server \\ __MODULE__, args) do
    GenServer.cast(server, {:foo, args})
  end
end

This gives me a lot of flexibility when testing the server and provides reuse. But, I don’t tend to use this pattern in libraries because it can create name conflicts if 2 different users try to use the default name. Passing the name around can definitely be annoying, but I haven’t found a better pattern that still provides the same level of flexibility. At this point I’m used to passing the name or a pid as the first argument to my function calls.

Likewise :slight_smile:

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews