Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use gen_server in Erlang/OTP applications

Having read Joe Armstrong's book and watched Kevin Smith screencasts I've built a simple OTP application comprised of a single gen_server and single supervisor, bundled together as an OTP application.

Now I'm looking at mochiweb and have created a sample project [helloworld] using the new_mochiweb.erl script. Browsing the source code I see it's not dissimilar from my sample OTP app [the OTP application is there, the supervisor is there] with one key difference .. the generated helloworld.erl and helloworld_web.erl files don't implement gen_server behaviour, they are just standard Erlang modules.

I was under the impression that using gen_server was the recommended way to go when building OTP application components. Why might mochiweb use OTP application and supervisor behaviours but eschew gen_server ?

like image 947
Justin Avatar asked Aug 31 '09 20:08

Justin


2 Answers

You use OTP/gen_servers for processes which are under a restart strategy - that is the restart of them is in your control.

That is not the case with processes representing connections to web browsers. If that process dies there is no way for the server to restart it - therefore running it under OTP is pointless.

Mochiweb (and Yaws) both use gen_servers to bind to the listening port and then spawn an unsupervised process to handle a new connection.

like image 80
Gordon Guthrie Avatar answered Oct 14 '22 23:10

Gordon Guthrie


There is a gen_server, called mochiweb_socket_server. The generated modules are only "callback modules" for the gen_server to be called when recieveing an incoming request.

like image 35
Zed Avatar answered Oct 15 '22 00:10

Zed