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:
- Should we, as library authors, strive to always provide a multi-instance configuration?
- Are there other ways to do this cleanly? Even if that demands Elixir 1.9+…
Cheers!