2

new to Erlang and just having a bit of trouble getting my head around the new paradigm!

OK, so I have this internal function within an OTP gen_server:

my_func() ->
Result = ibrowse:send_req(?ROOTPAGE,[{"User-Agent",?USERAGENT}],get),
case Result of
    {ok, "200", _, Xml} -> %<<do some stuff that won't interest you>>
,ok;
{error,{conn_failed,{error,nxdomain}}} -> <<what the heck do I do here?>>
end.

If I leave out the case for handling the connection failed then I get an exit signal propagated to the supervisor and it gets shut down along with the server.

What I want to happen (at least I think this is what I want to happen) is that on a connection failure I'd like to pause and then retry send_req say 10 times and at that point the supervisor can fail.

If I do something ugly like this...

{error,{conn_failed,{error,nxdomain}}} -> stop()

it shuts down the server process and yes, I get to use my (try 10 times within 10 seconds) restart strategy until it fails, which is also the desired result however the return value from the server to the supervisor is 'ok' when I would really like to return {error,error_but_please_dont_fall_over_mr_supervisor}.

I strongly suspect in this scenario that I'm supposed to handle all the business stuff like retrying failed connections within 'my_func' rather than trying to get the process to stop and then having the supervisor restart it in order to try it again.

Question: what is the 'Erlang way' in this scenario ?

1 Answer 1

2

I'm new to erlang too.. but how about something like this?

The code is long just because of the comments. My solution (I hope I've understood correctly your question) will receive the maximum number of attempts and then do a tail-recursive call, that will stop by pattern-matching the max number of attempts with the next one. Uses timer:sleep() to pause to simplify things.

%% @doc Instead of having my_func/0, you have
%% my_func/1, so we can "inject" the max number of
%% attempts. This one will call your tail-recursive
%% one
my_func(MaxAttempts) ->
    my_func(MaxAttempts, 0).

%% @doc This one will match when the maximum number
%% of attempts have been reached, terminates the
%% tail recursion.
my_func(MaxAttempts, MaxAttempts) ->
    {error, too_many_retries};

%% @doc Here's where we do the work, by having
%% an accumulator that is incremented with each
%% failed attempt.
my_func(MaxAttempts, Counter) ->
    io:format("Attempt #~B~n", [Counter]),
    % Simulating the error here.
    Result = {error,{conn_failed,{error,nxdomain}}},
    case Result of
        {ok, "200", _, Xml} -> ok;
        {error,{conn_failed,{error,nxdomain}}} ->
            % Wait, then tail-recursive call.
            timer:sleep(1000),
            my_func(MaxAttempts, Counter + 1)
    end.

EDIT: If this code is in a process which is supervised, I think it's better to have a simple_one_for_one, where you can add dinamically whatever workers you need, this is to avoid delaying initialization due to timeouts (in a one_for_one the workers are started in order, and having sleep's at that point will stop the other processes from initializing).

EDIT2: Added an example shell execution:

1> c(my_func).
my_func.erl:26: Warning: variable 'Xml' is unused
{ok,my_func}
2> my_func:my_func(5).
Attempt #0
Attempt #1
Attempt #2
Attempt #3
Attempt #4
{error,too_many_retries}

With 1s delays between each printed message.

4
  • Yes, I tried something similar (slightly less elegant than your solution), passing in the number of attempts then decrementing and testing for zero. The only difference is that I called stop() rather than return a tuple, stop will shut down the server (and have it restarted) but not return a useful message, {error,too_many_retries} will return a useful message but not shut down the server. Jun 20, 2012 at 4:15
  • I was hoping to find a solution to get a combination of both worlds, as the send_req error appears I retry 10 times and then if it still fails I catch it and then throw it again but in such a manner as to avoid causing the supervisor to fail as well - however I also think that this is not the 'Erlang way'. I think your solution is the way to go because if any other exception occurs it should be covered by the restart strategy of the supervisor. If anyone else has a different approach or opinion then by all means put it through. many thanks Jun 20, 2012 at 4:16
  • @unclejimbob From what I understood, you want to make the supervisor fail only when the max attempts have been reached,correct? Then make the supervisor restart the worker so the whole operation is retried from the top. This is what I tried to do with the code. You might want to complement it with an erlang:error(too_many_attempts). So you only want to "let it crash" when the max attempts have been reached or an unknown error has been thrown (not catched). What I dont like about timer:sleep() is that your worker is not obeying sys messages, so another solution would be a receive with a timeout
    – marcelog
    Jun 20, 2012 at 11:59
  • or maybe using a gen_server and timeouts... anyone? :)
    – marcelog
    Jun 20, 2012 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.