Spaces:
Sleeping
Sleeping
Scott Hiett
commited on
Commit
•
249b683
1
Parent(s):
abcde98
HOTFIX: Redis connections not getting properly destroyed
Browse files
lib/srh/redis/client_registry.ex
CHANGED
@@ -48,9 +48,10 @@ defmodule Srh.Redis.ClientRegistry do
|
|
48 |
{:ok, pid},
|
49 |
%{
|
50 |
state_update
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
}
|
55 |
}
|
56 |
end
|
@@ -72,16 +73,17 @@ defmodule Srh.Redis.ClientRegistry do
|
|
72 |
:noreply,
|
73 |
%{
|
74 |
state
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
}
|
79 |
}
|
80 |
end
|
81 |
|
82 |
def handle_cast({:destroy_workers}, state) do
|
83 |
for worker_pid <- state.worker_pids do
|
84 |
-
|
85 |
end
|
86 |
|
87 |
{:noreply, %{state | worker_pids: [], last_worker_index: 0}}
|
@@ -89,8 +91,10 @@ defmodule Srh.Redis.ClientRegistry do
|
|
89 |
|
90 |
def handle_cast({:return_worker, pid}, state) do
|
91 |
# Remove it from the borrowed array
|
92 |
-
{
|
93 |
-
|
|
|
|
|
94 |
end
|
95 |
|
96 |
def handle_cast(_msg, state) do
|
|
|
48 |
{:ok, pid},
|
49 |
%{
|
50 |
state_update
|
51 |
+
|
|
52 |
+
currently_borrowed_pids:
|
53 |
+
[pid | state_update.currently_borrowed_pids]
|
54 |
+
|> Enum.uniq()
|
55 |
}
|
56 |
}
|
57 |
end
|
|
|
73 |
:noreply,
|
74 |
%{
|
75 |
state
|
76 |
+
|
|
77 |
+
worker_pids:
|
78 |
+
[pid | state.worker_pids]
|
79 |
+
|> Enum.uniq()
|
80 |
}
|
81 |
}
|
82 |
end
|
83 |
|
84 |
def handle_cast({:destroy_workers}, state) do
|
85 |
for worker_pid <- state.worker_pids do
|
86 |
+
Srh.Redis.ClientWorker.destroy_redis(worker_pid)
|
87 |
end
|
88 |
|
89 |
{:noreply, %{state | worker_pids: [], last_worker_index: 0}}
|
|
|
91 |
|
92 |
def handle_cast({:return_worker, pid}, state) do
|
93 |
# Remove it from the borrowed array
|
94 |
+
{
|
95 |
+
:noreply,
|
96 |
+
%{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)}
|
97 |
+
}
|
98 |
end
|
99 |
|
100 |
def handle_cast(_msg, state) do
|
lib/srh/redis/client_worker.ex
CHANGED
@@ -21,6 +21,10 @@ defmodule Srh.Redis.ClientWorker do
|
|
21 |
GenServer.call(worker, {:redis_command, command_array})
|
22 |
end
|
23 |
|
|
|
|
|
|
|
|
|
24 |
def handle_call({:redis_command, command_array}, _from, %{redix_pid: redix_pid} = state)
|
25 |
when is_pid(redix_pid) do
|
26 |
case Redix.command(redix_pid, command_array) do
|
@@ -36,6 +40,14 @@ defmodule Srh.Redis.ClientWorker do
|
|
36 |
{:reply, :ok, state}
|
37 |
end
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def handle_cast(_msg, state) do
|
40 |
{:noreply, state}
|
41 |
end
|
|
|
21 |
GenServer.call(worker, {:redis_command, command_array})
|
22 |
end
|
23 |
|
24 |
+
def destroy_redis(worker) do
|
25 |
+
GenServer.cast(worker, {:destroy_redis})
|
26 |
+
end
|
27 |
+
|
28 |
def handle_call({:redis_command, command_array}, _from, %{redix_pid: redix_pid} = state)
|
29 |
when is_pid(redix_pid) do
|
30 |
case Redix.command(redix_pid, command_array) do
|
|
|
40 |
{:reply, :ok, state}
|
41 |
end
|
42 |
|
43 |
+
def handle_cast({:destroy_redis}, %{redix_pid: redix_pid} = state) when is_pid(redix_pid) do
|
44 |
+
# Destroy the redis instance & ensure cleanup
|
45 |
+
Redix.stop(redix_pid)
|
46 |
+
Process.exit(redix_pid, :normal)
|
47 |
+
|
48 |
+
{:stop, :normal, %{state | redix_pid: nil}}
|
49 |
+
end
|
50 |
+
|
51 |
def handle_cast(_msg, state) do
|
52 |
{:noreply, state}
|
53 |
end
|