Skip to content

Commit

Permalink
Add ability to not automatically reconnect.
Browse files Browse the repository at this point in the history
This is useful because the automatic reconnect spawns a process that is not linked
and could run forever (if the server is permanently unavailable). Robust clients
will need to have their own reconnect logic anyway to handle failures with the
initial connect.
  • Loading branch information
Ransom Richardson committed Jul 9, 2012
1 parent 4803877 commit c80568e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion include/eredis.hrl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%% Public types

-type option() :: {host, string()} | {port, integer()} | {database, string()} | {password, string()} | {reconnect_sleep, integer()}.
-type reconnect_sleep() :: no_reconnect | integer().

-type option() :: {host, string()} | {port, integer()} | {database, string()} | {password, string()} | {reconnect_sleep, reconnect_sleep()}.
-type server_args() :: [option()].

-type return_value() :: undefined | binary() | [binary()].
Expand Down
2 changes: 1 addition & 1 deletion src/eredis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ start_link(Host, Port, Database, Password, ReconnectSleep)
is_integer(Port);
is_integer(Database);
is_list(Password);
is_integer(ReconnectSleep) ->
is_integer(ReconnectSleep) orelse ReconnectSleep =:= no_reconnect ->

eredis_client:start_link(Host, Port, Database, Password, ReconnectSleep).

Expand Down
10 changes: 7 additions & 3 deletions src/eredis_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
port :: integer() | undefined,
password :: binary() | undefined,
database :: binary() | undefined,
reconnect_sleep :: integer() | undefined,
reconnect_sleep :: reconnect_sleep() | undefined,

socket :: port() | undefined,
parser_state :: #pstate{} | undefined,
Expand All @@ -54,7 +54,7 @@
Port::integer(),
Database::integer(),
Password::string(),
ReconnectSleep::integer()) ->
ReconnectSleep::reconnect_sleep()) ->
{ok, Pid::pid()} | {error, Reason::term()}.
start_link(Host, Port, Database, Password, ReconnectSleep) ->
gen_server:start_link(?MODULE, [Host, Port, Database, Password, ReconnectSleep], []).
Expand Down Expand Up @@ -106,9 +106,13 @@ handle_info({tcp, _Socket, Bs}, State) ->
{noreply, handle_response(Bs, State)};

%% Socket got closed, for example by Redis terminating idle
%% clients. Spawn of a new process which will try to reconnect and
%% clients. If desired, spawn of a new process which will try to reconnect and
%% notify us when Redis is ready. In the meantime, we can respond with
%% an error message to all our clients.
handle_info({tcp_closed, _Socket}, #state{reconnect_sleep = no_reconnect} = State) ->
%% If we aren't going to reconnect, then there is nothing else for this process to do.
{stop, normal, State#state{socket = undefined}};

handle_info({tcp_closed, _Socket}, State) ->
Self = self(),
spawn(fun() -> reconnect_loop(Self, State) end),
Expand Down
2 changes: 1 addition & 1 deletion src/eredis_sub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ start_link(Host, Port, Password, ReconnectSleep,
when is_list(Host) andalso
is_integer(Port) andalso
is_list(Password) andalso
is_integer(ReconnectSleep) andalso
is_integer(ReconnectSleep) orelse ReconnectSleep =:= no_reconnect andalso
(is_integer(MaxQueueSize) orelse MaxQueueSize =:= infinity) andalso
(QueueBehaviour =:= drop orelse QueueBehaviour =:= exit) ->

Expand Down
8 changes: 6 additions & 2 deletions src/eredis_sub_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
-spec start_link(Host::list(),
Port::integer(),
Password::string(),
ReconnectSleep::integer(),
ReconnectSleep::reconnect_sleep(),
MaxQueueSize::integer(),
QueueBehaviour::drop | exit) ->
{ok, Pid::pid()} | {error, Reason::term()}.
Expand Down Expand Up @@ -164,9 +164,13 @@ handle_info({tcp, _Socket, Bs}, State) ->
end;

%% Socket got closed, for example by Redis terminating idle
%% clients. Spawn of a new process which will try to reconnect and
%% clients. If desired, spawn of a new process which will try to reconnect and
%% notify us when Redis is ready. In the meantime, we can respond with
%% an error message to all our clients.
handle_info({tcp_closed, _Socket}, #state{reconnect_sleep = no_reconnect} = State) ->
%% If we aren't going to reconnect, then there is nothing else for this process to do.
{stop, normal, State#state{socket = undefined}};

handle_info({tcp_closed, _Socket}, State) ->
Self = self(),
send_to_controller({eredis_disconnected, Self}, State),
Expand Down

0 comments on commit c80568e

Please sign in to comment.