dimitarvp
Advice how to go about library configuration from Elixir 1.9 and on?
Hey everybody,
In light of the recent Elixir 1.9 announcement, I’d like the first library I write to not use Mix.Config. (It is an sqlite3 library with an Ecto 3 adapter.) To that end, I’ve come to several alternatives while thinking and sketching code, and I am curious what anybody has to say as a preference or recommendation.
-
Process dictionary. I really like this but it comes with the arrangement that the user of the library will either (a) take special care to only use designated processes to interact with my library, or (b) will use it from anywhere they want but have to clean up the process dictionary keys after they are done with a task. As much as I like the process dictionary, it does really sound like going back to imperative languages where there’s an implied context / state to watch out for.
-
Have a
GenServerthat centralises the access to the limited resource my library is managing (connections / open handles to a sqlite3 database [file]), and include helper functions that merge config options to theGenServer’s state (a map). Clean, beautiful, makes perfect use of the OTP. -
Make the handle object used to initiate any operation in the library also encompass configuration. This I also like quite a bit since it’s a pure FP approach and doesn’t rely on implied context / state.
Other ideas are welcome as well.
I’m leaning towards starting with (3) because that will give the users of the library a GenServer-less use case. And then adding (2) because then the users of the library can also just name their sqlite connection and the OTP will take care of dispatching their calls to the appropriate GenServer.
Again, I like (1) a lot but it doesn’t feel right to me.
Opinions? Ideas? Criticisms?
Most Liked
tristan
For runtime configuration of your library you simply document the configuration options that the user will put in their releases.exs.
I can’t say I’ve seen this much in Elixir so I may be off base here but in Erlang we put defaults in the .app/.app.src file under the env key. You can do the same with the application configuration in your mix.exs.
But this is all assuming a use case that makes sense to have configuration read in from the environment. It depends on the application whether that makes sense or if it is a case better to have the user passing in a a set of configuration values when calling a function in your application.
Also your (2) and (3) aren’t mutually exclusive. If you have an application that must start servers you likely want to give the user a way to start them in their own supervision tree and have them pass in configuration to the start_link of whatever they are starting from your application.
(1) should never be used :). Not that the pdict should never be used, just almost never, but for configuration I think it is safe to say just “never”.
wojtekmach
Definitely pass the data around. If you use global configuration (e.g. application env) it means
you have less flexibility, you only have one configuration and so you can’t configure this
connection with X and that connection with Y.
As others have mentioned you likely have some startlink/init/connect/new/whatever function, so accept configuration there and pass it around to further calls. And if passing options is annoying to the users of the library they can (and often should) wrap the library with their own module.
LostKobrakai
I don’t think your options 1/2 really hit the nail for what the guidelines and removal of /config are meant to push people to.
If you have a library and no (configurable) processes are being started => accept config as parameters to functions. This could be supplying fresh config to each call, but also a config = MyApp.Config.validate(input) token to be passed around.
If you have processes being started, which need user configuration, let the user start those processes in their applications supervision tree and accept config as start arguments. Let your library functions receive a pid/registered name as argument to know which processes to call into (like e.g. GenServer.call). This isn’t really limited to a single process as well. What the user interacts with could just be a top level supervisor with many children.
If you have done the above things you can look into making configuration more convenient and less manual by additionally harnessing e.g. the application environment, process dictionary or having compile time config provided via macros (like e.g. Ecto.Repo does it). You could compile simpler API functions into some modules (MyModule.func(…) instead of Lib.func(MyModule, …)). This allows user to use those “limiting” configurations options if it fit’s their usage, but they can still go the more manual way and configure stuff via parameters/start arguments for more complex usecases.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









