Permalink
Browse files

Make the reconnect sleep time configurable and not 100ms hardcoded

  • Loading branch information...
1 parent 0ff8af2 commit 9b028e77c3fb296bef83bb1b7d2386927e223ed4 @dialtone dialtone committed Sep 29, 2011
Showing with 15 additions and 7 deletions.
  1. +15 −7 src/eredis_client.erl
View
@@ -25,7 +25,7 @@
-include("eredis.hrl").
%% API
--export([start_link/4, stop/1, select_database/2]).
+-export([start_link/4, start_link/5, stop/1, select_database/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -36,14 +36,14 @@
port :: integer() | undefined,
password :: binary() | undefined,
database :: binary() | undefined,
+ reconnect_sleep :: integer() | undefined,
socket :: port() | undefined,
parser_state :: #pstate{} | undefined,
queue :: queue() | undefined
}).
-define(SOCKET_OPTS, [binary, {active, once}, {packet, raw}, {reuseaddr, true}]).
--define(RECONNECT_SLEEP, 100). %% Sleep between reconnect attempts, in milliseconds
%%
%% API
@@ -52,7 +52,13 @@
-spec start_link(Host::list(), Port::integer(), Database::integer(),
Password::string()) -> {ok, Pid::pid()} | {error, Reason::term()}.
start_link(Host, Port, Database, Password) ->
- gen_server:start_link(?MODULE, [Host, Port, Database, Password], []).
+ start_link(Host, Port, Database, Password, 100).
+
+-spec start_link(Host::list(), Port::integer(), Database::integer(),
+ Password::string(), ReconnectSleep::integer()) -> {ok, Pid::pid()} | {error, Reason::term()}.
+start_link(Host, Port, Database, Password, ReconnectSleep) ->
+ gen_server:start_link(?MODULE, [Host, Port, Database, Password, ReconnectSleep], []).
+
stop(Pid) ->
gen_server:call(Pid, stop).
@@ -61,11 +67,13 @@ stop(Pid) ->
%% gen_server callbacks
%%====================================================================
-init([Host, Port, Database, Password]) ->
+init([Host, Port, Database, Password, ReconnectSleep]) ->
State = #state{host = Host,
port = Port,
database = list_to_binary(integer_to_list(Database)),
password = list_to_binary(Password),
+ reconnect_sleep = ReconnectSleep,
+
parser_state = eredis_parser:init(),
queue = queue:new()},
@@ -238,18 +246,18 @@ do_sync_command(Socket, Command) ->
%% @doc: Loop until a connection can be established, this includes
%% successfully issuing the auth and select calls. When we have a
%% connection, give the socket to the redis client.
-reconnect_loop(Client, State) ->
+reconnect_loop(Client, #state{reconnect_sleep=ReconnectSleep}=State) ->
case catch(connect(State)) of
{ok, #state{socket = Socket}} ->
gen_tcp:controlling_process(Socket, Client),
Client ! {connection_ready, Socket};
{error, _Reason} ->
- timer:sleep(?RECONNECT_SLEEP),
+ timer:sleep(ReconnectSleep),
reconnect_loop(Client, State);
%% Something bad happened when connecting, like Redis might be
%% loading the dataset and we got something other than 'OK' in
%% auth or select
_ ->
- timer:sleep(?RECONNECT_SLEEP),
+ timer:sleep(ReconnectSleep),
reconnect_loop(Client, State)
end.

0 comments on commit 9b028e7

Please sign in to comment.