Skip to content

Commit

Permalink
Do not crash on timeout reading elasticache conf (#47)
Browse files Browse the repository at this point in the history
* Do not crash on timeout reading elasticache conf

* Include the endpoint that failed on the exception

* Improve specs at src/mero_elasticache.erl

Co-Authored-By: elbrujohalcon <[email protected]>
  • Loading branch information
polvorin and elbrujohalcon committed Apr 4, 2019
1 parent 304f0cc commit 7b37d90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/mero_conf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ get_elasticache_cluster_config(Host, Port) ->

get_elasticache_cluster_config(_Host, _Port, _Retries, {ok, Entries}) ->
Entries;
get_elasticache_cluster_config(_Host, _Port, ?MAX_RETRIES, {error, Reason}) ->
error(Reason);
get_elasticache_cluster_config(Host, Port, ?MAX_RETRIES, {error, Reason}) ->
error({Reason, Host, Port});
get_elasticache_cluster_config(Host, Port, Retries, {error, _Reason}) ->
timer:sleep(trunc(math:pow(2, Retries)) * 100),
get_elasticache_cluster_config(
Expand Down
24 changes: 18 additions & 6 deletions src/mero_elasticache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,29 @@ request_response(Host, Port, Command, Names) ->
case gen_tcp:connect(Host, Port, Opts) of
{ok, Socket} ->
ok = gen_tcp:send(Socket, Command),
Lines = [{Name, begin
{ok, Line} = gen_tcp:recv(Socket, 0, 10000),
Line
end}
|| Name <- Names],
Lines = receive_lines(Names, Socket),
ok = gen_tcp:close(Socket),
{ok, Lines};
Lines;
Error ->
Error
end.

-spec receive_lines([atom()], gen_tcp:socket()) -> {ok, [{atom(), binary()}]} | {error, atom()}.
receive_lines(Names, Socket) ->
receive_lines(Names, Socket, []).


receive_lines([], _Socket, Lines) ->
{ok, lists:reverse(Lines)};

receive_lines([Name|Names], Socket, Acc) ->
case gen_tcp:recv(Socket, 0, 10000) of
{ok, Line} ->
receive_lines(Names, Socket, [{Name, Line}|Acc]);
{error, Error} ->
{error, Error}
end.

%% Parse host and version lines to return version and list of {host, port} cluster nodes
-spec parse_cluster_config(binary()) ->
{ok, Config :: [cluster_entry()]} | {error, Reason :: atom()}.
Expand Down

0 comments on commit 7b37d90

Please sign in to comment.