[erlang-questions] Applications, supervisors, and servers, oh my

Bernard Duggan bernie@REDACTED
Wed May 26 01:35:04 CEST 2010


On 25/05/10 22:50, David Welton wrote:
> I suppose registering the process names with
> a value the user supplies is the best way of giving them a way to
> continue interacting with the system once it's been started.
>    
That's certainly one option, though I've rarely seen it used (though 
that in itself is not an argument against doing it).  More common is for 
a process to register with a name it knows, then provide interface 
functions to access it.  So if, for example, I have a gen_server module 
that I start with a function like this:

start_link(Args) ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, Args, []).

and I then want to, say, synchronously "add one to x", I'd provide that 
feature it in the same module as an exported function:

add_one_to_x(X) ->
     gen_server:call(?MODULE, {add_one_to_x, X}).

and as the related gen_server handle_call function:

handle_call({add_one_to_x, X}, _, State) ->
     % Do something with X and state
     {reply, ok, NewState};

Hopefully that makes sense.  Now the client only has to call

add_one_to_x(X),

Since you're limited to having one instance of any given application 
running on a given node, the fact that the application does its own 
process naming (rather than one being passed in) doesn't matter (and 
gives the client one less thing to keep track of).  This is a pretty 
common pattern throughout the OTP code (mnesia etc).
> However, I'm wondering about how to handle errors.  The code, as is,
> has all it needs to deal with pretty much any error.
> [snip]  The current system seems more fine-grained than what a supervisor could
> provide, or am I wrong?
>    
Error handling isn't something I'm really confident enough with yet to 
give you any firm advice - there's a bunch of other threads recently 
discussing this stuff that are probably worth a read.  The short answer 
for this case, though, is that supervisors deal with restarting (or not 
restarting) processes or groups of processes.  If you want error 
recovery to take place while maintaining the state of your process, then 
obviously that has to be done within the process itself.

Cheers,

Bernard


More information about the erlang-questions mailing list