Commits

Anonymous committed ee5cee4

notice when other messages are waiting and don't set to 0 the remaining count

Comments (0)

Files changed (1)

         orelse Cmd =:= sismember orelse Cmd =:= select orelse Cmd =:= move
         orelse Cmd =:= save orelse Cmd =:= bgsave orelse Cmd =:= shutdown ->
     fun proto:parse/2;
-
 get_parser(Cmd)
     when Cmd =:= incr orelse Cmd =:= incrby orelse Cmd =:= decr
         orelse Cmd =:= decrby orelse Cmd =:= llen orelse Cmd =:= scard ->
     fun proto:parse_int/2;
-
 get_parser(Cmd) when Cmd =:= type ->
     fun proto:parse_types/2;
-
 get_parser(Cmd) when Cmd =:= randomkey ->
     fun proto:parse_string/2;
-
 get_parser(Cmd)
     when Cmd =:= get orelse Cmd =:= lindex orelse Cmd =:= lpop
         orelse Cmd =:= rpop ->
     fun proto:single_stateful_parser/2;
-
 get_parser(Cmd)
     when Cmd =:= keys orelse Cmd =:= lrange orelse Cmd =:= sinter
         orelse Cmd =:= smembers ->
 
 connect(Host) ->
     connect(Host, 6379).
-
 connect(Host, Port) ->
     gen_server:start_link(?MODULE, [Host, Port], []).
 
 
 send(Client, Cmd) -> send(Client, Cmd, []).
 send(Client, Cmd, Args) ->
-    gen_server:call(Client, {send, string:join([str(Cmd), format(Args)], " "), get_parser(Cmd)}).
+    gen_server:call(Client, {send,
+        string:join([str(Cmd), format(Args)], " "), get_parser(Cmd)}).
 
 asend(Client, Cmd) ->
     gen_server:cast(Client, {send, Cmd}).
     {stop, normal, ok, State};
 handle_call(_, _From, State) -> {noreply, State}.
 
+
 handle_cast({send, Cmd}, State) ->
+    % how we should do here: if remaining is already != 0 then we'll
+    % let handle_info take care of keeping track how many remaining things
+    % there are. If instead it's 0 we are the first call so let's just
+    % do it.
     gen_tcp:send(State#redis.socket, [Cmd|?EOL]),
-    {noreply, State#redis{remaining=1}};
+    {noreply, State};
 handle_cast(_Msg, State) -> {noreply, State}.
 
+
 trim2({ok, S}) ->
     string:substr(S, 1, length(S)-2);
 trim2(S) ->
     trim2({ok, S}).
 
+% This is useful to know if 
+get_remaining(ParsersQueue) ->
+    case queue:is_empty(ParsersQueue) of
+        true -> 0;
+        false -> 1
+    end.
+
+
 handle_info({tcp, Socket, Data}, State) ->
     NewState = case State#redis.caller of
         undefined ->
                 % The error message that we will parse.
                 {0, error} ->
                     % reinsert the parser in the front, next step is still gonna be needed
-                    State#redis{remaining=1, pstate=error, parsers=queue:in_r(Parser, NewParsers)};
+                    State#redis{remaining=1, pstate=error,
+                            parsers=queue:in_r(Parser, NewParsers)};
 
                 % The stateful parser just started and tells us the number
                 % of results that we will have to parse for those calls
                     % Reset the remaining value to the number of results
                     % that we need to parse.
                     % and reinsert the parser in the front, next step is still gonna be needed
-                    State#redis{remaining=Remaining, pstate=read, parsers=queue:in_r(Parser, NewParsers)};
+                    State#redis{remaining=Remaining, pstate=read,
+                            parsers=queue:in_r(Parser, NewParsers)};
 
                 % We either had only one thing to read or we are at the
                 % end of the stuff that we need to read. either way
                     inet:setopts(Socket, [{packet, line}]), % go back to line mode
                     OldBuffer = State#redis.buffer,
                     gen_server:reply(From, lists:reverse([CurrentValue|OldBuffer])),
-                    State#redis{remaining=0, buffer=[], pstate=empty, caller=undefined, parsers=NewParsers};
+                    State#redis{remaining=get_remaining(NewParsers), buffer=[],
+                        pstate=empty, caller=undefined, parsers=NewParsers};
 
 
                 % The stateful parser tells us to read some bytes
                 % Simple return values contained in a single line
                 {0, Value} ->
                     gen_server:reply(From, Value),
-                    State#redis{remaining=0, pstate=empty, caller=undefined, buffer=[], parsers=NewParsers}
+                    State#redis{remaining=get_remaining(NewParsers),
+                        pstate=empty, caller=undefined, buffer=[],
+                        parsers=NewParsers}
             end
     end,
     inet:setopts(Socket, [{active, once}]),
     {noreply, NewState};
+handle_info(_Info, State) -> {noreply, State}.
 
-handle_info(_Info, State) -> {noreply, State}.
 
 terminate(_Reason, State) ->
     case State#redis.socket of
     end,
     ok.
 
+
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%