tags:

views:

325

answers:

2

[This will only make sense if you've seen Kevin Smith's 'Erlang in Practice' screencasts]

I'm an Erlang noob trying to build a simple Erlang/OTP system with embedded webserver [mochiweb].

I've walked through the EIP screencasts, and I've toyed with simple mochiweb examples created using the new_mochiweb.erl script.

I'm trying to figure out how the webserver should relate to the gen_server modules. In the EIP examples [Ch7], the author creates a web_server.erl gen_server process and links the mochiweb_http process to it. However in a mochiweb project, the mochiweb_http process seems to be 'standalone'; it doesn't seem to be embedded in a separate gen_server process.

My question is, should one of these patterns be preferred over the other ? If so, why ? Or doesn't it matter ?

Thanks in advance.

+1  A: 

The reason to embed a process in a supervision tree is so that you can restart it if it fails.

A process that handles an HTTP request is responding to an event generated externally - in a browser. It is not possible to restart it - that is the prerogative of the person running the browser - therefore it is not necessary to run it under OTP - you can just spawn it without supervision.

Gordon Guthrie
+2  A: 

You link processes to the supervisor hierarchy of your application for two reasons: 1) to be able to restart your worker processes if they crash, and 2) to be able to kill all your processes when you stop the application.

As the previous answer says, 1) is not the case for http requests handling processes. However, 2) is valid: if you let your processes alone, you can't guarantee that all your processes will be cleared from the VM after stopping your application (think of processes stuck in endless loops, waiting in receives, etc...).

Zed