Browse Source

gen_reply to reply gen_server calls

Anton Lebedevich 13 years ago
parent
commit
a885896ec7
1 changed files with 11 additions and 6 deletions
  1. 11 6
      src/pgsql_sock.erl

+ 11 - 6
src/pgsql_sock.erl

@@ -149,6 +149,11 @@ reply(#state{queue = Q} = State, Message) ->
     Pid ! Message,
     Pid ! Message,
     State#state{queue = Q2}.
     State#state{queue = Q2}.
 
 
+gen_reply(#state{queue = Q} = State, Message) ->
+    {{value, From}, Q2} = queue:out(Q),
+    gen_server:reply(From, Message),
+    State#state{queue = Q2}.
+
 notify_async(#state{async = Pid}, Msg) ->
 notify_async(#state{async = Pid}, Msg) ->
     case is_pid(Pid) of
     case is_pid(Pid) of
         true  -> Pid ! {pgsql, self(), Msg};
         true  -> Pid ! {pgsql, self(), Msg};
@@ -186,7 +191,7 @@ auth({$R, <<M:?int32, _/binary>>}, State) ->
         _ -> Method = unknown
         _ -> Method = unknown
     end,
     end,
     Error = {error, {unsupported_auth_method, Method}},
     Error = {error, {unsupported_auth_method, Method}},
-    {stop, Error, reply(State, Error)};
+    {stop, Error, gen_reply(State, Error)};
 
 
 %% ErrorResponse
 %% ErrorResponse
 auth({error, E}, State) ->
 auth({error, E}, State) ->
@@ -196,11 +201,11 @@ auth({error, E}, State) ->
         Any         -> Why = Any
         Any         -> Why = Any
     end,
     end,
     Error = {error, Why},
     Error = {error, Why},
-    {stop, Error, reply(State, Error)};
+    {stop, Error, gen_reply(State, Error)};
 
 
 auth(timeout, State) ->
 auth(timeout, State) ->
     Error = {error, timeout},
     Error = {error, timeout},
-    {stop, Error, reply(State, Error)};
+    {stop, Error, gen_reply(State, Error)};
 
 
 auth(Other, State) ->
 auth(Other, State) ->
     #state{timeout = Timeout} = State,
     #state{timeout = Timeout} = State,
@@ -215,7 +220,7 @@ initializing({$K, <<Pid:?int32, Key:?int32>>}, State) ->
 
 
 initializing(timeout, State) ->
 initializing(timeout, State) ->
     Error = {error, timeout},
     Error = {error, timeout},
-    {stop, Error, reply(State, Error)};
+    {stop, Error, gen_reply(State, Error)};
 
 
 %% ReadyForQuery
 %% ReadyForQuery
 initializing({$Z, <<Status:8>>}, State) ->
 initializing({$Z, <<Status:8>>}, State) ->
@@ -230,10 +235,10 @@ initializing({$Z, <<Status:8>>}, State) ->
     State2 = State#state{handler = on_message,
     State2 = State#state{handler = on_message,
                          txstatus = Status,
                          txstatus = Status,
                          ready = true},
                          ready = true},
-    {noreply, reply(State2, {ok, self()})};
+    {noreply, gen_reply(State2, {ok, self()})};
 
 
 initializing({error, _} = Error, State) ->
 initializing({error, _} = Error, State) ->
-    {stop, Error, reply(State, Error)};
+    {stop, Error, gen_reply(State, Error)};
 
 
 initializing(Other, State) ->
 initializing(Other, State) ->
     #state{timeout = Timeout} = State,
     #state{timeout = Timeout} = State,