Commits

Jacob Perkins committed 20a2cd1 Merge

merge

Comments (0)

Files changed (4)

 # store output so is only executed once
-LIBDIR=$(shell erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell)
+ERL_LIBS=$(shell erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell)
 # get application vsn from app file
 VSN=$(shell erl -pa ebin/ -eval 'application:load(erldis), {ok, Vsn} = application:get_key(erldis, vsn), io:format("~s~n", [Vsn])' -s init stop -noshell)
 
 	install ebin/erldis.appup $(ERL_LIBS)/erldis-$(VSN)/ebin/erldis.appup
 
 plt:
-	@dialyzer --build_plt --plt .plt -q -r . -I include/
+	@dialyzer --build_plt --output_plt .plt -q -r . -I include/
 
 check: all
 	@dialyzer --check_plt --plt .plt -q -r . -I include/

include/erldis.hrl

--record(redis, {socket,buffer=[],reply_caller,calls=0,remaining=0,pstate=empty,results=[]}).
+-record(redis, {socket,buffer=[],reply_caller,calls=0,remaining=0,pstate=empty,results=[], host, port, timeout}).

src/erldis_client.erl

             % japerk: correct error return is {stop, Reason}
             {stop, {socket_error, Why}};
         {ok, Socket} ->
-            {ok, #redis{socket=Socket, calls=0}}
+            {ok, #redis{socket=Socket, host=Host, port=Port, timeout=Timeout, calls=0}}
     end.
 
 handle_call({send, Cmd}, From, State) ->

src/erldis_sync_client.erl

 			V
 	end.
 
+ensure_started(#redis{socket=undefined, host=Host, port=Port, timeout=Timeout}=State)->
+	Opts = [list, {active, once}, {packet, line}, {nodelay, true}, {send_timeout, Timeout}],
+	
+	case gen_tcp:connect(Host, Port, Opts, Timeout) of
+		{ok, Socket} ->
+			error_logger:info_report([{?MODULE, reconnected}, State]),
+			State#redis{socket=Socket};
+		{error, Why} ->
+			Report = [{?MODULE, unable_to_connect}, {error, Why}, State],
+			error_logger:warning_report(Report),
+			State
+	end;
+ensure_started(State)->
+	State.
+
 %%%%%%%%%%%%%%%%%%
 %% call command %%
 %%%%%%%%%%%%%%%%%%
 			{stop, {socket_error, Why}};
 		{ok, Socket} ->
 			% calls is a queue instead of a count
-			{ok, #redis{socket=Socket, calls=queue:new()}}
+			{ok, #redis{socket=Socket, calls=queue:new(), host=Host, port=Port, timeout=Timeout}}
 	end.
 
 %%%%%%%%%%%%%%%%%
 %% handle_call %%
 %%%%%%%%%%%%%%%%%
 
-handle_call({send, Cmd}, From, State) ->
+handle_call({send, Cmd}, From, State1) ->
 	% NOTE: redis ignores sent commands it doesn't understand, which means
 	% we don't get a reply, which means callers will timeout
+	State = ensure_started(State1),
 	case gen_tcp:send(State#redis.socket, [Cmd|?EOL]) of
 		ok ->
 			%error_logger:info_report([{send, Cmd}, {from, From}]),
 
 parse_state(State, Socket, Data) ->
 	Parse = erldis_proto:parse(State#redis.pstate, trim2(Data)),
-	
 	case {State#redis.remaining-1, Parse} of
 		{0, error} ->
 			% next line is the error string
 	end;
 handle_info({tcp_closed, Socket}, State=#redis{socket=Socket}) ->
 	error_logger:warning_report([{erldis_sync_client, tcp_closed}, State]),
-	{stop, shutdown, State};
+	{noreply, State#redis{socket=undefined}};
 handle_info(_Info, State) ->
 	{noreply, State}.