How to increase the default timeout of GenServer elixir?

The default is probably 5 secs. Can someone please tell me how to increase this?

You just need to add a timeout… https://hexdocs.pm/elixir/GenServer.html#call/3

2 Likes

I knw that but what is the syntax to enhance that?

I am using like this:
def init(initial_data) do
{:ok, initial_data, timeout(5000000)}
end

It gives me this error:
undefined function timeout/1

{:ok, initial_data, 5_000_000}

1 Like

That timeout is used to detect a lull between messages:

Also I suspect that the timeout has to be renewed on each handle_* return value - so it doesn’t act as a “default”.

This is entirely unrelated to the timeout parameter on GenServer.call/3:

5 Likes

Just to confirm You need to renew on each call, and You receive a timeout message after delay that You can use to close the server.

  @impl GenServer
  def handle_info(:timeout, state), do: {:stop, {:shutdown, :timeout}, state}

If done wrong, You could see your workers shuting down automatically :slight_smile:

1 Like

As you always should expose a wrapper around GenServer.call/3 and never see calls to it somewhere else, that wrapper needs to specify the timeout:

def foo(arg), do: GenServer.calll(__MODULE__, {:foo, arg}, 10_000)

This will use a timeout of 10 seconds.

6 Likes

Beware this would not work if you need several instances of __MODULE__.

1 Like

Consider following GenServer.call/3’s example and give the client a way to change the timeout if necessary.

def foo(server, arg, timeout \\ 10_000), 
  do: GenServer.call(server, {:foo, arg}, timeout)

Now foo/3’s default timeout is 10 seconds.

4 Likes