[erlang-questions] proper use of application:set_env?

Ulf Wiger ulf.wiger@REDACTED
Fri Nov 13 19:53:08 CET 2009


Pablo Platt wrote:
> Hi,
> 
> I have an application that start a mochiweb server.
> The same application handles several vhosts.
> Each vhost has a config file with dispatch-list + handlers, db-name, secret-string...
> The handlers are modules which need to know about the settings in the config file, for example the db name or the sercret-string to authenticate users.
> I don't want to access the config file every time because of performance.
> 
> Is it ok to use 
> application:set_env('example.com_setting1', "some setting1") ,
> application:set_env('example.com_setting2', "some setting2"),
> application:set_env('other-domain.com_setting1', "some setting1") ...
> and then access it from the handlers?

It's not recommended, but technically, I don't really
think there is a problem. The thing to remember is that many
programs will check the environment variables once and assume
they will not change after that. One needs to make certain that
changes of the environment variables are reflected in the running
programs.


> Is there a performance price for using application_get_env for every request?

If you specify the application name, it is one ets lookup.
If you don't, it's two ets lookups - one to find the current
application, based on the mapping of group leader to app,
and then a lookup to find the env variable.

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com



More information about the erlang-questions mailing list