Skip to content

Commit 1b2415c

Browse files
committed
Improve handling for multi exec commands when unable to connect to the Redis server
1 parent bb41a4c commit 1b2415c

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

lib/srh/http/command_handler.ex

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,35 @@ defmodule Srh.Http.CommandHandler do
9595
# Borrow a client, then run all of the commands (wrapped in MULTI and EXEC)
9696
worker_pid = Client.borrow_worker(client_pid)
9797

98-
wrapped_command_array = [["MULTI"] | command_array]
99-
do_dispatch_command_transaction_array(wrapped_command_array, worker_pid, responses)
100-
101-
# Now manually run the EXEC - this is what contains the information to form the response, not the above
102-
result =
103-
case ClientWorker.redis_command(worker_pid, ["EXEC"]) do
104-
{:ok, res} ->
105-
{
106-
:ok,
107-
res
108-
|> Enum.map(&%{result: &1})
109-
}
110-
111-
{:error, error} ->
112-
decode_error(error)
113-
end
114-
115-
Client.return_worker(client_pid, worker_pid)
116-
117-
result
98+
# We are manually going to invoke the MULTI, because there might be a connection error to the Redis server.
99+
# In that case, we don't want the error to be wound up in the array of errors,
100+
# we instead want to return the error immediately.
101+
case ClientWorker.redis_command(worker_pid, ["MULTI"]) do
102+
{:ok, _} ->
103+
do_dispatch_command_transaction_array(command_array, worker_pid, responses)
104+
105+
# Now manually run the EXEC - this is what contains the information to form the response, not the above
106+
result =
107+
case ClientWorker.redis_command(worker_pid, ["EXEC"]) do
108+
{:ok, res} ->
109+
{
110+
:ok,
111+
res
112+
|> Enum.map(&%{result: &1})
113+
}
114+
115+
{:error, error} ->
116+
decode_error(error)
117+
end
118+
119+
Client.return_worker(client_pid, worker_pid)
120+
121+
# Fire back the result here, because the initial Multi was successful
122+
result
123+
124+
{:error, error} ->
125+
decode_error(error)
126+
end
118127

119128
{:error, msg} ->
120129
{:server_error, msg}

lib/srh/http/request_validator.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ defmodule Srh.Http.RequestValidator do
3434
defp do_validate_encoding_header([first_item | rest]) do
3535
case first_item do
3636
"base64" -> {:ok, true}
37-
3837
_ -> do_validate_encoding_header(rest)
3938
end
4039
end

lib/srh/http/result_encoder.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule Srh.Http.ResultEncoder do
2-
32
# Authentication errors don't get encoded, we need to skip over those
43
def encode_response({:not_authorized, message}) do
54
{:not_authorized, message}
@@ -27,12 +26,16 @@ defmodule Srh.Http.ResultEncoder do
2726
## RESULT LIST ENCODING ##
2827

2928
defp encode_response_list([current | rest], encoded_responses) do
30-
encoded_current_entry = case current do
31-
%{result: value} ->
32-
%{result: encode_result_value(value)} # Encode the value
33-
%{error: error_message} ->
34-
%{error: error_message} # We don't encode errors
35-
end
29+
encoded_current_entry =
30+
case current do
31+
%{result: value} ->
32+
# Encode the value
33+
%{result: encode_result_value(value)}
34+
35+
%{error: error_message} ->
36+
# We don't encode errors
37+
%{error: error_message}
38+
end
3639

3740
encode_response_list(rest, [encoded_current_entry | encoded_responses])
3841
end

lib/srh/redis/client_registry.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ defmodule Srh.Redis.ClientRegistry do
4848
{:ok, pid},
4949
%{
5050
state_update
51-
|
52-
currently_borrowed_pids:
53-
[pid | state_update.currently_borrowed_pids]
54-
|> Enum.uniq()
51+
| currently_borrowed_pids:
52+
[pid | state_update.currently_borrowed_pids]
53+
|> Enum.uniq()
5554
}
5655
}
5756
end
@@ -73,10 +72,9 @@ defmodule Srh.Redis.ClientRegistry do
7372
:noreply,
7473
%{
7574
state
76-
|
77-
worker_pids:
78-
[pid | state.worker_pids]
79-
|> Enum.uniq()
75+
| worker_pids:
76+
[pid | state.worker_pids]
77+
|> Enum.uniq()
8078
}
8179
}
8280
end

0 commit comments

Comments
 (0)