benonymus

benonymus

Hey there,

I created a small genserver and it works great but I want to add a timeout to a call, and to test it in the handle_call/3 I am doing Process.sleep/1 with a longer time then the timeout, but instead of getting a :timeout message to catch in handle_info/2 I am just getting an error that says:

** (exit) exited in: GenServer.call(Checksum.Checksum, :calculate, 50)
** (EXIT) time out

here is the call:

  def calculate_checksum() do
    GenServer.call(__MODULE__, :calculate, 50)
  end

here is the handle:

  @impl GenServer
  def handle_call(:calculate, _, state) do
    Process.sleep(60)
    result = 0

    {:reply, {:ok, result}, state}
  end

this is where I would expect to catch the timeout:

  @impl GenServer
  def handle_info(:timeout, state) do
    {:reply, {:error, :timeout}, state}
  end

What am I missing? :smile:

Thanks

Showing Posts 1 to 10

kokolegorille

kokolegorille

Where is your timeout defined?

benonymus

benonymus OP

The 50 I believe

kokolegorille

kokolegorille

No, it’s should not be like that…

It’s when You return from any call that You specify timeout.

like {:reply, state, state, timeout}

benonymus

benonymus OP

kokolegorille

kokolegorille

It is not the same timeout. One is the client timeout, one is the server timeout.

Please note server handlers should also return timeout, even init.

kokolegorille

kokolegorille

Here is an example…

  @timeout 5 * 60 * 1_000

  @impl GenServer
  def handle_call(:get_state, _from, state), do: {:reply, state, state, @timeout}

  # Timeout handler
  @impl GenServer
  def handle_info(:timeout, state), do: stop_and_clean(state, {:shutdown, :timeout})

stop and clean is a custom function…

benonymus

benonymus OP

And how can I make this timeout?

What I actually want to use is to show a different response if the timeout happens.
Now I added the timeout to where you said, and I am getting the :timeout message.
But where I am making the call to the genserver I am still getting the result.

with {:ok, checksum} <- Checksum.calculate_checksum() do
      conn
      |> put_status(200)
      |> put_view(ChecksumView)
      |> render("checksum.json", checksum: checksum, message: "Checksum calculated successfully!")
    else
      _ ->
        conn
        |> put_status(488)
        |> put_view(ChecksumView)
        |> render("408.json")
    end

As you saw from my handle_call for :calculate I am returning {:ok, result}, but I am also seeing the :timeout message being handled.

  @impl GenServer
  def handle_info(:timeout, state) do
    {:noreply, state}
  end

What would be ideal if there is a timeout instead of getting {:ok, result} I could get something like {:error, :timeout} but If I do reply with this error and state in the handle_info then it is a bad return value.

benonymus

benonymus OP

I think I was on the right path based on this: gen_server — OTP 29.0.2 (stdlib 8.0.1)
And I think I have this problem:
https://groups.google.com/g/elixir-lang-talk/c/7tS9tX7fLpg

I tried to put the genserver call in try do but no success yet

amnu3387

amnu3387

When you do a call with a timeout this is the timeout the caller process will wait before it throws an exception. You need to rescue this exception at the call site.

The timeout in a gen_server (the one you can set in the callback of any handle) is an internal timeout that sets an amount of time the gen_server can be without receiving any message, which upon it elapsing triggers an internal message of :timeout that you can handle. This timeout is disabled whenever a message arrives in the gen_servers mailbox and needs to be set again if you wish it to trigger again.

benonymus

benonymus OP

Hey, thanks for the response!

I am trying this:

  def calculate_checksum() do
    try do
      GenServer.call(__MODULE__, :calculate, 15)
    rescue
      _ -> {:error, :timeout}
    end
  end

but no success

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews