Scott Hiett commited on
Commit
d1b22d9
·
unverified ·
2 Parent(s): 739b573 f085bb2

Merge pull request #19 from hiett/18-if-the-redis-server-goes-down-and-a-closed-is-returned-the-pool-isnt-properly-destroyed

Browse files
lib/srh/http/base_router.ex CHANGED
@@ -26,7 +26,10 @@ defmodule Srh.Http.BaseRouter do
26
  end
27
 
28
  match _ do
29
- handle_response({:not_found, "SRH: Endpoint not found. SRH might not support this feature yet."}, conn)
 
 
 
30
  end
31
 
32
  defp do_command_request(conn, success_lambda) do
 
26
  end
27
 
28
  match _ do
29
+ handle_response(
30
+ {:not_found, "SRH: Endpoint not found. SRH might not support this feature yet."},
31
+ conn
32
+ )
33
  end
34
 
35
  defp do_command_request(conn, success_lambda) do
lib/srh/http/command_handler.ex CHANGED
@@ -130,6 +130,11 @@ defmodule Srh.Http.CommandHandler do
130
  # Fire back the result here, because the initial Multi was successful
131
  result
132
 
 
 
 
 
 
133
  {:error, error} ->
134
  decode_error(error, srh_id)
135
  end
@@ -175,6 +180,12 @@ defmodule Srh.Http.CommandHandler do
175
  {:ok, res} ->
176
  {:ok, %{result: res}}
177
 
 
 
 
 
 
 
178
  {:error, error} ->
179
  decode_error(error, srh_id)
180
  end
 
130
  # Fire back the result here, because the initial Multi was successful
131
  result
132
 
133
+ {:error, %{reason: :closed} = error} ->
134
+ # Ensure that this pool is killed, but still pass the error up the chain for the response
135
+ Client.destroy_workers(client_pid)
136
+ decode_error(error, srh_id)
137
+
138
  {:error, error} ->
139
  decode_error(error, srh_id)
140
  end
 
180
  {:ok, res} ->
181
  {:ok, %{result: res}}
182
 
183
+ # Jedix connection error
184
+ {:error, %{reason: :closed} = error} ->
185
+ # Ensure that this pool is killed, but still pass the error up the chain for the response
186
+ Client.destroy_workers(pid)
187
+ decode_error(error, srh_id)
188
+
189
  {:error, error} ->
190
  decode_error(error, srh_id)
191
  end
lib/srh/http/content_type_check_plug.ex CHANGED
@@ -35,7 +35,10 @@ defmodule Srh.Http.ContentTypeCheckPlug do
35
  # Return a custom error, ensuring the same format as the other errors
36
  conn
37
  |> put_resp_content_type("application/json")
38
- |> send_resp(400, Jason.encode!(%{error: "Invalid content type. Expected application/json."}))
 
 
 
39
  |> halt()
40
  end
41
  end
 
35
  # Return a custom error, ensuring the same format as the other errors
36
  conn
37
  |> put_resp_content_type("application/json")
38
+ |> send_resp(
39
+ 400,
40
+ Jason.encode!(%{error: "Invalid content type. Expected application/json."})
41
+ )
42
  |> halt()
43
  end
44
  end
lib/srh/redis/client.ex CHANGED
@@ -3,7 +3,7 @@ defmodule Srh.Redis.Client do
3
  alias Srh.Redis.ClientRegistry
4
  alias Srh.Redis.ClientWorker
5
 
6
- @idle_death_time 1000 * 15
7
 
8
  def start_link(max_connections, connection_info) do
9
  GenServer.start_link(__MODULE__, {max_connections, connection_info}, [])
@@ -35,6 +35,10 @@ defmodule Srh.Redis.Client do
35
  GenServer.cast(client, {:return_worker, pid})
36
  end
37
 
 
 
 
 
38
  def handle_call({:find_worker}, _from, %{registry_pid: registry_pid} = state)
39
  when is_pid(registry_pid) do
40
  {:ok, worker} = ClientRegistry.find_worker(registry_pid)
@@ -59,13 +63,17 @@ defmodule Srh.Redis.Client do
59
  {:noreply, state}
60
  end
61
 
 
 
 
 
 
62
  def handle_cast(_msg, state) do
63
  {:noreply, state}
64
  end
65
 
66
  def handle_info(:idle_death, state) do
67
  ClientRegistry.destroy_workers(state.registry_pid)
68
-
69
  {:stop, :normal, state}
70
  end
71
 
 
3
  alias Srh.Redis.ClientRegistry
4
  alias Srh.Redis.ClientWorker
5
 
6
+ @idle_death_time 1000 * 60 * 15 # 15 minutes
7
 
8
  def start_link(max_connections, connection_info) do
9
  GenServer.start_link(__MODULE__, {max_connections, connection_info}, [])
 
35
  GenServer.cast(client, {:return_worker, pid})
36
  end
37
 
38
+ def destroy_workers(client) do
39
+ GenServer.cast(client, {:destroy_workers})
40
+ end
41
+
42
  def handle_call({:find_worker}, _from, %{registry_pid: registry_pid} = state)
43
  when is_pid(registry_pid) do
44
  {:ok, worker} = ClientRegistry.find_worker(registry_pid)
 
63
  {:noreply, state}
64
  end
65
 
66
+ def handle_cast({:destroy_workers}, state) do
67
+ ClientRegistry.destroy_workers(state.registry_pid)
68
+ {:stop, :normal, state}
69
+ end
70
+
71
  def handle_cast(_msg, state) do
72
  {:noreply, state}
73
  end
74
 
75
  def handle_info(:idle_death, state) do
76
  ClientRegistry.destroy_workers(state.registry_pid)
 
77
  {:stop, :normal, state}
78
  end
79
 
lib/srh/redis/client_worker.ex CHANGED
@@ -62,9 +62,29 @@ defmodule Srh.Redis.ClientWorker do
62
  } = state
63
  )
64
  when is_binary(connection_string) do
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # NOTE: Redix only seems to open the connection when the first command is sent
66
  # This means that this will return :ok even if the connection string may not actually be connectable
67
- {:ok, pid} = Redix.start_link(connection_string)
 
 
 
 
 
68
  {:noreply, %{state | redix_pid: pid}}
69
  end
70
 
 
62
  } = state
63
  )
64
  when is_binary(connection_string) do
65
+ enable_ssl = String.starts_with?(connection_string, "rediss://")
66
+
67
+ socket_opts =
68
+ case enable_ssl do
69
+ true ->
70
+ [
71
+ customize_hostname_check: [
72
+ match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
73
+ ]
74
+ ]
75
+
76
+ false ->
77
+ []
78
+ end
79
+
80
  # NOTE: Redix only seems to open the connection when the first command is sent
81
  # This means that this will return :ok even if the connection string may not actually be connectable
82
+ {:ok, pid} =
83
+ Redix.start_link(connection_string,
84
+ ssl: enable_ssl,
85
+ socket_opts: socket_opts
86
+ )
87
+
88
  {:noreply, %{state | redix_pid: pid}}
89
  end
90