From 249b683776256f9f0e0804bd650640a24e599e6e Mon Sep 17 00:00:00 2001 From: Scott Hiett Date: Thu, 8 Sep 2022 00:21:21 +0100 Subject: [PATCH] HOTFIX: Redis connections not getting properly destroyed --- lib/srh/redis/client_registry.ex | 22 +++++++++++++--------- lib/srh/redis/client_worker.ex | 12 ++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/srh/redis/client_registry.ex b/lib/srh/redis/client_registry.ex index 2a0a2bc..adfb1ff 100644 --- a/lib/srh/redis/client_registry.ex +++ b/lib/srh/redis/client_registry.ex @@ -48,9 +48,10 @@ defmodule Srh.Redis.ClientRegistry do {:ok, pid}, %{ state_update - | currently_borrowed_pids: - [pid | state_update.currently_borrowed_pids] - |> Enum.uniq() + | + currently_borrowed_pids: + [pid | state_update.currently_borrowed_pids] + |> Enum.uniq() } } end @@ -72,16 +73,17 @@ defmodule Srh.Redis.ClientRegistry do :noreply, %{ state - | worker_pids: - [pid | state.worker_pids] - |> Enum.uniq() + | + worker_pids: + [pid | state.worker_pids] + |> Enum.uniq() } } end def handle_cast({:destroy_workers}, state) do for worker_pid <- state.worker_pids do - Process.exit(worker_pid, :normal) + Srh.Redis.ClientWorker.destroy_redis(worker_pid) end {:noreply, %{state | worker_pids: [], last_worker_index: 0}} @@ -89,8 +91,10 @@ defmodule Srh.Redis.ClientRegistry do def handle_cast({:return_worker, pid}, state) do # Remove it from the borrowed array - {:noreply, - %{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)}} + { + :noreply, + %{state | currently_borrowed_pids: List.delete(state.currently_borrowed_pids, pid)} + } end def handle_cast(_msg, state) do diff --git a/lib/srh/redis/client_worker.ex b/lib/srh/redis/client_worker.ex index 976f5b3..a0364e4 100644 --- a/lib/srh/redis/client_worker.ex +++ b/lib/srh/redis/client_worker.ex @@ -21,6 +21,10 @@ defmodule Srh.Redis.ClientWorker do GenServer.call(worker, {:redis_command, command_array}) end + def destroy_redis(worker) do + GenServer.cast(worker, {:destroy_redis}) + end + def handle_call({:redis_command, command_array}, _from, %{redix_pid: redix_pid} = state) when is_pid(redix_pid) do case Redix.command(redix_pid, command_array) do @@ -36,6 +40,14 @@ defmodule Srh.Redis.ClientWorker do {:reply, :ok, state} end + def handle_cast({:destroy_redis}, %{redix_pid: redix_pid} = state) when is_pid(redix_pid) do + # Destroy the redis instance & ensure cleanup + Redix.stop(redix_pid) + Process.exit(redix_pid, :normal) + + {:stop, :normal, %{state | redix_pid: nil}} + end + def handle_cast(_msg, state) do {:noreply, state} end