Sharing [config] data through modules, functions

I have some configuration data in the configuration file that I read from disk when the application starts. I need to make this configuration data available to other functions / modules in the application. I started the path to browsing in ets / mnesia to store data at startup to make it common to all processes, but then my inner voice warned me that there should be a more functional, erlang-y way to do this. The only alternative approach I came to is to create a module in which there is an actor loop that reads data at startup and answers messages such as {Key, From} and answers From! {OK, Value}. Then I gave up and decided to ask ... Thanks, --tim

+4
source share
2 answers

If you only need some configuration options, you can include them as environment variables (in Erlang terms) in one of your Erlang applications. The way to do this is to include them in the .app file (or .app.src) of your application in the env tuple:

Sort of:

 {application, ch_app, [{description, "Channel allocator"}, {vsn, "1"}, {modules, [ch_app, ch_sup, ch3]}, {registered, [ch3]}, {applications, [kernel, stdlib, sasl]}, {mod, {ch_app,[]}}, {env, [{file, "/usr/local/log"}]} ]}. 

SOURCE: http://www.erlang.org/doc/design_principles/applications.html

As you can see, file is a configuration variable. You can access the variable with:

 application:get_env(ch_app, file). 

If you need something more complex, you may need to create a gen_server process that responds to all configuration requests (getter and setter methods).

+4
source

Not to mention that any simple self-recording solution involves reading the configuration file before starting the main dispatcher, as you may need these variables. Just a thought, but I ran into the same problem in my own code.

0
source

Source: https://habr.com/ru/post/1305015/


All Articles