File size: 2,306 Bytes
2e92879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249b683
 
 
 
2e92879
 
 
 
 
 
8155c92
2e92879
 
 
 
 
76486d3
2e92879
 
 
249b683
 
 
 
 
 
 
 
2e92879
 
 
 
 
 
 
 
 
 
 
 
 
f085bb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8155c92
 
f085bb2
 
 
 
 
 
2e92879
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
defmodule Srh.Redis.ClientWorker do
  use GenServer

  def start_link(connection_info) do
    GenServer.start_link(__MODULE__, connection_info, [])
  end

  def init(connection_info) do
    Process.send(self(), :create_connection, [])

    {
      :ok,
      %{
        connection_info: connection_info,
        redix_pid: nil
      }
    }
  end

  def redis_command(worker, command_array) 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
      {:ok, res} ->
        {:reply, {:ok, res}, state}

      # Both connection errors and Redis command errors will be handled here
      {:error, res} ->
        {:reply, {:error, res}, state}
    end
  end

  def handle_call(_msg, _from, state) 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

  def handle_info(
        :create_connection,
        %{
          connection_info: %{
            "connection_string" => connection_string
          }
        } = 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,
        ssl: enable_ssl,
        socket_opts: socket_opts
      )

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

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