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
5 changes: 4 additions & 1 deletion lib/srh/http/base_router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ defmodule Srh.Http.BaseRouter do
end

match _ do
handle_response({:not_found, "SRH: Endpoint not found. SRH might not support this feature yet."}, conn)
handle_response(
{:not_found, "SRH: Endpoint not found. SRH might not support this feature yet."},
conn
)
end

defp do_command_request(conn, success_lambda) do
Expand Down
11 changes: 11 additions & 0 deletions lib/srh/http/command_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ defmodule Srh.Http.CommandHandler do
# Fire back the result here, because the initial Multi was successful
result

{:error, %{reason: :closed} = error} ->
# Ensure that this pool is killed, but still pass the error up the chain for the response
Client.destroy_workers(client_pid)
decode_error(error, srh_id)

{:error, error} ->
decode_error(error, srh_id)
end
Expand Down Expand Up @@ -175,6 +180,12 @@ defmodule Srh.Http.CommandHandler do
{:ok, res} ->
{:ok, %{result: res}}

# Jedix connection error
{:error, %{reason: :closed} = error} ->
# Ensure that this pool is killed, but still pass the error up the chain for the response
Client.destroy_workers(pid)
decode_error(error, srh_id)

{:error, error} ->
decode_error(error, srh_id)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/srh/http/content_type_check_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ defmodule Srh.Http.ContentTypeCheckPlug do
# Return a custom error, ensuring the same format as the other errors
conn
|> put_resp_content_type("application/json")
|> send_resp(400, Jason.encode!(%{error: "Invalid content type. Expected application/json."}))
|> send_resp(
400,
Jason.encode!(%{error: "Invalid content type. Expected application/json."})
)
|> halt()
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/srh/redis/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Srh.Redis.Client do
alias Srh.Redis.ClientRegistry
alias Srh.Redis.ClientWorker

@idle_death_time 1000 * 15
@idle_death_time 1000 * 60 * 15 # 15 minutes

def start_link(max_connections, connection_info) do
GenServer.start_link(__MODULE__, {max_connections, connection_info}, [])
Expand Down Expand Up @@ -35,6 +35,10 @@ defmodule Srh.Redis.Client do
GenServer.cast(client, {:return_worker, pid})
end

def destroy_workers(client) do
GenServer.cast(client, {:destroy_workers})
end

def handle_call({:find_worker}, _from, %{registry_pid: registry_pid} = state)
when is_pid(registry_pid) do
{:ok, worker} = ClientRegistry.find_worker(registry_pid)
Expand All @@ -59,13 +63,17 @@ defmodule Srh.Redis.Client do
{:noreply, state}
end

def handle_cast({:destroy_workers}, state) do
ClientRegistry.destroy_workers(state.registry_pid)
{:stop, :normal, state}
end

def handle_cast(_msg, state) do
{:noreply, state}
end

def handle_info(:idle_death, state) do
ClientRegistry.destroy_workers(state.registry_pid)

{:stop, :normal, state}
end

Expand Down
22 changes: 21 additions & 1 deletion lib/srh/redis/client_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,29 @@ defmodule Srh.Redis.ClientWorker do
} = state
)
when is_binary(connection_string) do
enable_ssl = String.starts_with?(connection_string, "rediss://")

socket_opts =
case enable_ssl do
true ->
[
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
]
]

false ->
[]
end

# NOTE: Redix only seems to open the connection when the first command is sent
# This means that this will return :ok even if the connection string may not actually be connectable
{:ok, pid} = Redix.start_link(connection_string)
{:ok, pid} =
Redix.start_link(connection_string,
ssl: enable_ssl,
socket_opts: socket_opts
)

{:noreply, %{state | redix_pid: pid}}
end

Expand Down