[newbie] accept()-ing process under supervision (was:Erlangish way of Iterator pattern)

Gaspar Chilingarov nm@REDACTED
Wed Jan 26 16:20:26 CET 2005


ok, i think that i got idea :)

anyway, sorry for making to much noise on the list, but i'm trying to 
prototype some network management system/console in Erlang and move it 
away from perl/php/shell/C/etc. because it's easy to programm in that 
languages, but hard enough to provide stability/scalability/ease of 
maintenance.

hmm . last phrase sounds a little bit ugly, but i'm not a native speaker :)

Joe Armstrong (AL/EAB) wrote:
> Tricky - there is a spectrum of answers here:
> 
> "make sure that it is always listening" could mean lot's of different things:
> 
> 	1) Is the listening process (ie the process which evaluates tcp_accept)
>                 still alive?
> 	2) Is the listening process responsive?
> 	    ie is it accepting connections and doing what it is supposed to do?
> 
> In my example tcp_server might happily accept connections and spawn a new handler
> per connection - but these handlers might deadlock, or go into infinite loops or something and not respond as
> expected (Now I don't mean here that the handlers *crash* - because crashes will be detected and the
> socket will be closed) - it's just that they don't work properly.
> 
> Detecting 1) is trivial (just link to the process). 2) requires some kind of end-to-end confirmation
> ie a program *outside* Erlang which periodically asks the server to prove that it running. 
> Say ask it what factorial 42 is once every minute.
> 
> <<to be really safe - the probing program should be on a *different* processor -
>     because of "Joe's law"
> 	
> 	+------------+
> 	| Joe's law |
> 	+-----------------------------------------+
> 	|  To do fault-tolerant computations |
>             | you need at least 2 computers      |
> 	+-----------------------------------------+
> 
>      Obviously :-)
>    
> 
> 
> 
> An easy solution (somewhere between 1 and 2 here) can be to use "on-exit" defined thus:
> 
> on_exit(Pid, Fun) ->
>          spawn_fun(fun() -> 
>                           process_flag(trap_exit, true),
>                           link(Pid),
>                           receive
>                               {'EXIT', Pid, Why} ->
>                                   Fun(Why)
>                           end
>                    end).
> 
> You can use on_exit to restart a function if it fails.
> 
> [almost] like this:
> 
> keep_alive(Fun) ->
> 	Pid = spawn(fun() -> Fun() end),
> 	on_exit(Pid, 
> 		fun({'Exit', normal} ->
> 			true;
> 		       {'EXIT', Other} ->
> 			%% restart it
> 			keep_alive(Fun)
> 		end).
> 
> Cheers
> 
> /Joe
> 
> [almost] - the code above is incorrect - and is merely to illustrate the idea -
> why is it incorrect? (ten brownie points for the first correct explanation of the error :-)
> 
> 
> 
> 
> 
> 



More information about the erlang-questions mailing list