zzebra

zzebra

Handle GRPC channel on client side

Hi all,
I’m creating a GRPC Client using grpc that deals only with unary RPCs.
I want to use the same gRPC channel as much as possible and not open new ones for each request.
So I decided to create a GenServer that is responsible for

  • creating the channel on app start
  • keeping the channel open and try to reopen if it was lost for some reason

My goal was to keep my app running when the gRPC Server is down and be able to re-create the connection once the Server is back again.

Genserver code

defmodule App.GRPCChannel do
  use GenServer
  require Logger

  alias App.Settings

  # client
  def start_link(_) do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def channel() do
    case GenServer.call(__MODULE__, :channel) do
      %GRPC.Channel{} = channel -> {:ok, channel}
      _ -> {:error, :no_connection}
    end
  end

  # server
  @impl true
  def init(_) do
    initialize_channel()
  end

  @impl true
  def handle_info({:gun_down, _, _, _, _}, _state) do
    Logger.debug("GRPC Server not connected.")

    {:noreply, :no_connection}
  end

  @impl true
  def handle_info({:gun_up, _, _, _, _}, state) do
    Logger.debug("GRPC Server connected")

    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_up, pid, _}, :no_connection) do
    Process.exit(pid, :kill)

    Logger.debug("GRPC Client killed zombie Gun process, pid: #{inspect(pid)}")

    {:noreply, :no_connection}
  end

  @impl true
  def handle_info({:gun_up, pid, _}, state) do
    if pid != state.adapter_payload.conn_pid do
      Process.exit(pid, :kill)
      Logger.debug("GRPC Client killed zombie Gun process, pid: #{inspect(pid)}")
    end

    {:noreply, state}
  end

  @impl true
  def handle_call(:channel, _from, channel) do
    case channel do
      %GRPC.Channel{} = channel -> {:reply, channel, channel}
      :no_connection -> restart_channel()
    end
  end

  defp restart_channel() do
    case initialize_channel() do
      {:ok, :no_connection} -> {:noreply, :no_connection}
      {:ok, channel} -> {:reply, channel, channel}
    end
  end

  defp initialize_channel() do
    address = Settings.iam_service_url()
    opts = Settings.iam_service_connection_opts()

    Logger.debug("GRPC Client connecting to gateway at #{address}")

    case GRPC.Stub.connect(address, opts) do
      {:error, error} ->
        Logger.critical("GRPC Client could not connect to GRPC Server. Message: #{error}")
        {:ok, :no_connection}

      {:ok, channel} ->
        Logger.debug("GRPC Client connected to the gateway at #{address}, using channel: #{inspect(channel)}")
        {:ok, channel}
    end
  end
end

The main ideas are

  • to only try to re-create the connection once channel() is called and it is a :no_connection.
  • use the default gun adapter and its messages to get info about the inner http server state - and based on that return have the channel itself or :no_connection as Genserver state.

When a gRPC request is fired it starts with fetching the channel every time, like this:

  def authenticate(resource_id, access_token) do
    with {:ok, %GRPC.Channel{} = channel} <- get_grpc_channel(),
         {:ok, %AuthenticateRequest{} = request} <- build_authenticate_request(resource_id, access_token),
         {:ok, %AuthenticateResponse{} = response} <- Stub.authenticate(channel, request) do
      {:ok, %{success: response.success, user_id: response.user_id}}
    else
      {:error, :iam_service} -> {:error, :iam_service}
      {:error, %GRPC.RPCError{} = _error} -> {:error, :unauthenticated}
    end
  end

  defp get_grpc_channel() do
    try do
      GRPCChannel.channel()
    catch
      :exit, _ -> {:error, :iam_service}
    end
  end

I need to catch the Genserver’s error here since it may exit when it couldn’t create a channel.

By trial and error (switching on and off the GRPC server) I noticed that the GRPC server might bring down my entire app due to the Supervisor retry logic, so I try to avoid it.
I also noticed that after restarting the GRPC server, the old gun process might re-connect and come back while the new one is also alive, that’s why I’m killing it, to keep only 1 alive.

I’m wondering how bad ideas are these…

All feedbacks are welcomed.

First Post!

D4no0

D4no0

You might want to create a backoff strategy for your supervisor (or you can add that logic in your genserver), here is a small example of how this can be achieved: erlang - Supervisors with backoff - Stack Overflow

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement