Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/srh/redis/client_registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,25 +73,28 @@ 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}}
end

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
Expand Down
12 changes: 12 additions & 0 deletions lib/srh/redis/client_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down