Permalink
Browse files

Merge pull request #30 from talko/master

Add option to not reconnect and timeouts on initial connection
  • Loading branch information...
2 parents 500523e + f1a9f02 commit 9acabdbf4c4fc32e788418b797bc8ea9c0fa2370 @knutin knutin committed Nov 6, 2012
Showing with 22 additions and 10 deletions.
  1. +5 −1 include/eredis.hrl
  2. +1 −1 src/eredis.erl
  3. +8 −4 src/eredis_client.erl
  4. +1 −1 src/eredis_sub.erl
  5. +7 −3 src/eredis_sub_client.erl
View
@@ -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()].
@@ -25,3 +27,5 @@
-define(NL, "\r\n").
-define(SOCKET_OPTS, [binary, {active, once}, {packet, raw}, {reuseaddr, true}]).
+
+-define(RECV_TIMEOUT, 5000).
View
@@ -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).
View
@@ -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,
@@ -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], []).
@@ -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),
@@ -263,7 +267,7 @@ do_sync_command(Socket, Command) ->
case gen_tcp:send(Socket, Command) of
ok ->
%% Hope there's nothing else coming down on the socket..
- case gen_tcp:recv(Socket, 0) of
+ case gen_tcp:recv(Socket, 0, ?RECV_TIMEOUT) of
{ok, <<"+OK\r\n">>} ->
inet:setopts(Socket, [{active, once}]),
ok;
View
@@ -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) ->
@@ -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()}.
@@ -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),
@@ -301,7 +305,7 @@ do_sync_command(Socket, Command) ->
case gen_tcp:send(Socket, Command) of
ok ->
%% Hope there's nothing else coming down on the socket..
- case gen_tcp:recv(Socket, 0) of
+ case gen_tcp:recv(Socket, 0, ?RECV_TIMEOUT) of
{ok, <<"+OK\r\n">>} ->
ok;
Other ->

0 comments on commit 9acabdb

Please sign in to comment.