[erlang-questions] Ranch and cleaning after client

Max Tretyakov emailyama@REDACTED
Sat Jun 20 22:10:34 CEST 2015

Hi all,
I am using ranch to accept/handle clients in our app, but I have some feelings that there is some scenario when I can’t handle cleaning up after client disconnection/tcp_error/idle timeout in terminate callback.
I am using ranch_protocol in way(simplified):

init(Ref, Socket, Transport, _Opts = []) ->
	ok = proc_lib:init_ack({ok, self()}),
	ok = ranch:accept_ack(Ref),
    	process_flag(trap_exit, true),
	gen_server:enter_loop(?MODULE, [], 
              #client{socket=Socket, transport=Transport}, ?IDLE_SOCKET_TIMEOUT). %1 min

handle_info({tcp_closed, _Socket}, State=#client{user = User}) ->
  %closed by client
{stop, normal, State};
handle_info({'EXIT', FromPid, Reason}, State) ->
    %exit signal 
{stop, normal, State};
handle_info({tcp_error, _, Reason}, State) ->
{stop, Reason, State};
handle_info(timeout, State=#client{user = User}) ->
{stop, normal, State};
handle_info(_Info, State=#client{user = User}) -> 
%catch all
{stop, normal, State}.
terminate(Reason, _State=#client{user = User, auth = Auth, socket = Socket}) ->
    case Auth =:= true andalso User =/= undefined of
        true -> users:quite(User);
        false -> ok
    end, ok.

In users:quite module — it is simple ets table with connections counter(increment/decrement, and deleting when connections get <= 0).

My questions: there is a scenario when cleaning up after client couldn’t happen in my case ? I now 1 possible variant, when you stopping listener, and in some situation supervisor could kill process, so cleaning never happen, but we don’t use this feature. Any advice how make correct cleaning up handling ?

Thank you!

More information about the erlang-questions mailing list