[erlang-questions] Module config data and functional programming

Daniel Dormont dan@REDACTED
Thu Apr 21 20:54:18 CEST 2011


Hi, I have been learning Erlang for about two months and I'm just now building my first real module, and I'm running into this question. The module is for Ejabberd, but I think that's incidental to my main question. Ejabberd offers a behavior called gen_mod which calls for two functions, start/2 and stop/1, which in my example look kind of like this:

start(Host, Opts) -> ejabberd_hooks:add(some_hook, global, ?MODULE, some_function).
stop(Host) -> ejabberd_hooks:delete(some_hook, global, ?MODULE, some_function).

some_function(List) -> lists:filter(fun my_filter/1, List).

my_filter(Value) -> %complex logic here%

The idea being that the module can be initialized with some options that were passed from a config file. FYI, ejabberd_hooks is used to register and unregister callbacks for certain events. 

What I have above works fine, but now I'm in a situation where my_filter needs access to data that came from Opts. I could convert the module to gen_server and store the Opts as my initial state, have some_function use gen_server:call, and inside my handle_call pass the appropriate elements from my StateData to the filter function my_filter. But I'm hesitant to do this for two reasons:

- the StateData will never change because there really is no mutable state in this module. This seems like a misuse of gen_server.
- because there's no state, I'd like to allow multiple simultaneous calls to some_function for speed reasons, and have the module stay as a library module.

Am I wrong about these? Is this a perfectly normal use of gen_server? Is forcing all calls into a single process not actually a bottleneck? Or is there another way of doing this? Would ETS be useful, or would that eliminate the supposed advantages I'm trying to gain?

FWIW, ejabberd_hooks does allow an actual function to be passed instead of an atom containing a function name.

thanks,
Dan


More information about the erlang-questions mailing list