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

Roberto Ostinelli roberto@REDACTED
Fri Jan 9 20:36:15 CET 2015


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150109/4b67f263/attachment.htm>


More information about the erlang-questions mailing list