Search code examples
erlanginets

Erlang: when to perform `inets:start()`?


What is the appropriate place for performing inets:start() ?

  1. in `applicationname_app' module?
  2. in applicationname_sup supervisor module?
  3. in a child process hanging from the supervisor?\
  4. someplace else?

(I am still struggling with inets:httpd)

Note: the answer cannot be to the tune " use a boot file " , please.


Solution

  • inets is a "stand-alone" Erlang application; inets:start() is just an alias to application:start(inets). I guess the answer pretty much depends on how you maintain your code.

    If your code is packaged as an application, your .app file should list inets as required to be started before yours (see the applications tag). Starting your applicaion with application:start(my_app). will now ensure that inets is also started. Consequence: if you make a boot file, it will also start inets for you :-P

    If you are keen on not using applications, I guess the choice depends on how your code works. If you will always need inets to be started, it is better started by any of your supervisors. If it is rarely needed, you can always make sure it is started with something like:

    ensure_app_started(App) ->
      case application:started(App) of
        ok -> ok;
        {error, already_started} -> ok;
        Error -> Error
      end.