Skip to content

Commit 249b683

Browse files
committed
HOTFIX: Redis connections not getting properly destroyed
1 parent abcde98 commit 249b683

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/srh/redis/client_registry.ex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ defmodule Srh.Redis.ClientRegistry do
4848
{:ok, pid},
4949
%{
5050
state_update
51-
| currently_borrowed_pids:
52-
[pid | state_update.currently_borrowed_pids]
53-
|> Enum.uniq()
51+
|
52+
currently_borrowed_pids:
53+
[pid | state_update.currently_borrowed_pids]
54+
|> Enum.uniq()
5455
}
5556
}
5657
end
@@ -72,25 +73,28 @@ defmodule Srh.Redis.ClientRegistry do
7273
:noreply,
7374
%{
7475
state
75-
| worker_pids:
76-
[pid | state.worker_pids]
77-
|> Enum.uniq()
76+
|
77+
worker_pids:
78+
[pid | state.worker_pids]
79+
|> Enum.uniq()
7880
}
7981
}
8082
end
8183

8284
def handle_cast({:destroy_workers}, state) do
8385
for worker_pid <- state.worker_pids do
84-
Process.exit(worker_pid, :normal)
86+
Srh.Redis.ClientWorker.destroy_redis(worker_pid)
8587
end
8688

8789
{:noreply, %{state | worker_pids: [], last_worker_index: 0}}
8890
end
8991

9092
def handle_cast({:return_worker, pid}, state) do
9193
# Remove it from the borrowed array
92-
{:noreply,
93-
%{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)}}
94+
{
95+
:noreply,
96+
%{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)}
97+
}
9498
end
9599

96100
def handle_cast(_msg, state) do

lib/srh/redis/client_worker.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ defmodule Srh.Redis.ClientWorker do
2121
GenServer.call(worker, {:redis_command, command_array})
2222
end
2323

24+
def destroy_redis(worker) do
25+
GenServer.cast(worker, {:destroy_redis})
26+
end
27+
2428
def handle_call({:redis_command, command_array}, _from, %{redix_pid: redix_pid} = state)
2529
when is_pid(redix_pid) do
2630
case Redix.command(redix_pid, command_array) do
@@ -36,6 +40,14 @@ defmodule Srh.Redis.ClientWorker do
3640
{:reply, :ok, state}
3741
end
3842

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+
3951
def handle_cast(_msg, state) do
4052
{:noreply, state}
4153
end

0 commit comments

Comments
 (0)