Interrupting passive socket recv

Samuel Tardieu <>
Thu Dec 14 17:58:26 CET 2000


On 14/12, Sean Hinde wrote:

| It's still pretty horrible to kill or have to timeout all the time though.
| What do you set your timeout to out of interest?

I use 3 seconds in mod_erl, but I really disklike that (one of the first
things I teach my students is to *never* do any busy waiting or things
like that).

This leads to this ugly structure, that allows the listening process to be
but in a supervision tree (with a non-neglectable shutdown time, since
it may take up to 3 seconds + epsilon to answer the system message).

listener (Parent, Debug, State) ->
    receive
        {system, From, Msg} ->
            sys:handle_system_msg (Msg, From, Parent, ?MODULE, Debug, State);
        {'EXIT', Parent, Reason} ->
            exit (Reason);
        {get_modules, From} ->
            From ! {modules, [?MODULE]},
            listener (Parent, Debug, State);
        _Other ->
            listener (Parent, Debug, State)
    after 0 ->
            case gen_tcp:accept (State#state.sock, 3000) of
                {ok, NS} ->
                    {ok, Pid} =
                        supervisor:start_child (eerl_workers,
                                                [NS, Debug]),
                    gen_tcp:controlling_process (NS, Pid),
                    listener (Parent, Debug, State);
                {error, timeout} ->
                    listener (Parent, Debug, State);
                {error, Reason} ->
                    exit (Reason)
            end
    end.
    





More information about the erlang-questions mailing list