elixirgenstage

use redis connection in elixir genstage consumer


I have an example of genstage application, and in its consumers I need to use a connection with redis. And I do not understand how I need to pass this connection to handle_events.

If I write:

  defp connection do
    {:ok, conn} = Redix.start_link(host: "0.0.0.0", port: 6379)
    conn
  end

Then every time the connection is called inside the handle_events function, it will create a new connection.

how can I solve this problem?


Solution

  • You can keep the conn in the state of the GenStage consumer (just like you would to in a GenServer, something like this:

    defmodule C do
      use GenStage
    
      def start_link() do
        GenStage.start_link(C, :ok)
      end
    
      def init(:ok) do
        {:ok, conn} = Redis.start_link(...)
        {:consumer, conn}
      end
    
      def handle_events(events, _from, conn) do
        Redix.command!(conn, ...)
    
        {:noreply, [], conn}
      end
    end
    

    Here I'm creating the connection when the consumer is created. You can also create the connection higher up and pass it down to this if you want, like this:

    defmodule C do
      use GenStage
    
      def start_link(conn) do
        GenStage.start_link(C, conn)
      end
    
      def init(conn) do
        {:consumer, conn}
      end
    
      def handle_events(events, _from, conn) do
        Redix.command!(conn, ...)
    
        {:noreply, [], conn}
      end
    end