[erlang-questions] Cowboy monitoring long-lived HTTP connections

Loïc Hoguin essen@REDACTED
Fri Jan 9 22:43:25 CET 2015


Terminate is not recommended for this and will become an optional 
callback because of how rarely it is actually useful. Cleaning up inside 
the process is never perfect because when a process is exited with 
reason 'kill' it ends immediately, with no way to call any terminate 
function.

A supervisor will use 'kill' when a child is configured with 
'brutal_kill' or when the shutdown timeout times out, for example. Then 
you end up with something that will never be cleaned up.

Monitoring is the right solution, but there's usually no need to do it 
yourself. Your registry, and the second solution in your email, is 
basically what gproc properties give you:

     https://github.com/uwiger/gproc#use-case-pubsub-patterns

Gproc will take care of monitoring, ets table and sending for you.

On 01/09/2015 08:36 PM, Roberto Ostinelli wrote:
> Dear list,
> I am using cowboy to manage long-lived HTTP requests, that I use to send
> server generated events.
>
> When a request comes in, I add it to a process registry. I can then send
> messages to the request process, that takes care of sending it down the
> TCP pipe.
>
> The question is: when one of these processes dies, also because of an
> internal crash, I want to ensure that I remove it's pid from the process
> registry.
>
> The question is: what is the best practice to do this? Is the terminate
> function called also when a process dies? If so, I could just do:
>
> init(_Type, Req, []) ->
>      ets:insert(?PID_TABLE, {self()}),
>      {ok, Req, undefined}.
>
> handle(Req, _ReqState) ->
>      [handle request]
>
> terminate(_Reason, _Req, _State) ->
>      ets:delete(?PID_TABLE, {self()}),
>      ok.
>
>
> Or do I need to monitor it with some kind of registry, for instance:
>
> init(_Type, Req, []) ->
>      registry:register(self()),
>      {ok, Req, undefined}.
>
> handle(Req, _ReqState) ->
>      [handle request]
>
> terminate(_Reason, _Req, _State) ->
>      ok.
>
>
> And registry is a gen_server:
>
> register(Pid) ->
> gen_server:call(?MODULE,{register, Pid}).
>
> ...
>
> handle_call({register, Pid}, _From, State) ->
> ets:insert(?PID_TABLE, {Pid}),
>      erlang:monitor(process, Pid),
>      {reply, ok, State};
>
> ...
>
> handle_info({'DOWN', _Ref, process, Pid, _Reason}, State) ->
>      ets:delete(?PID_TABLE, {Pid}),
>      {noreply, State}.
>
>
> Any recommendations?
>
> Thank you ^^_
> r.
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>

-- 
Loïc Hoguin
http://ninenines.eu



More information about the erlang-questions mailing list