vitorfam

vitorfam

Is it recommended to create a GenServer to make gRPC calls?

In my application, I need to make a lot of gRPC calls to a Python server. Is it recommended to create a GenServer to make those calls?

I’m trying to do the following:

defmodule MyApp.ClassifierServer do
  use GenServer

  alias MyApp.ClassifierImpl

  @name __MODULE__

  def start_link(_) do
    classifier_host = Application.get_env(:myapp, MyApp.ClassifierServer)[:hostname]
    {:ok, channel} = GRPC.Stub.connect(classifier_host)
    GenServer.start_link(@name, channel, name: @name)
  end

  def init(channel) do
    # Fazer uma verificação do status do serviço.
    IO.puts("Starting channel to Classifier Server")
    {:ok, channel}
  end

  def handle_call({:classify, params}, _from, channel) do
    case ClassifierImpl.classify(params, channel) do
      {:ok, response} ->
        {:reply, response, channel}

      {:error, error_message} ->
        {:reply, error_message, channel}
    end
  end
end

When I start my application, I get the following message:

[error] Unexpected event in state :connected of type :info:
{:timeout, #Reference<0.3273821823.2036334600.82725>, {:cow_http2_machine, :settings_timeout}}
{:state, #PID<0.604.0>, {:up, #Reference<0.3273821823.2036334600.82721>}, 'localhost', 50051, "http", 'localhost', 50051, [], %{protocols: [:http2], retry: 100, retry_fun: &GRPC.Adapter.Gun.retry_fun/2, tcp_opts: [nodelay: true], transport: :tcp}, #Reference<0.3273821823.2036334600.82726>, #Port<0.23>, :gun_tcp, true, {:tcp, :tcp_closed, :tcp_error}, :gun_http2, {:http2_state, #Port<0.23>, :gun_tcp, %{initial_connection_window_size: 8000000, initial_stream_window_size: 8000000}, [:gun_data_h], "", :connected, {:http2_machine, :client, %{initial_connection_window_size: 8000000, initial_stream_window_size: 8000000}, :normal, :undefined, #Reference<0.3273821823.2036334600.82725>, %{initial_window_size: 65535}, %{initial_window_size: 8000000}, %{initial_window_size: 4194304, max_frame_size: 4194304, max_header_list_size: 8192}, 4194304, 8000000, 1, 0, [], [], [], {:state, 0, 4096, 4096, []}, {:state, 0, 4096, 4096, []}}, %{}, %{}}, :gun_default_event_h, :undefined}

and the supervisor tries to restart the server every time I get this error.

Most Liked

tristan

tristan

Rebar3 Core Team

It probably depends on the grpc library. Looking at your example code I would not suggest the same when using grpcbox (https://github.com/tsloughter/grpcbox). grpcbox has client side load balancing and handles channels/subchannels for you, so keeping a connection in a genserver state is not the recommended way to go.

And while grpcbox is Erlang, I know there are production users who are using it from Elixir.

axelson

axelson

Scenic Core Team

Some good reading on this topic if you haven’t already read it is https://www.theerlangelist.com/article/spawn_or_not

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement