The default is probably 5 secs. Can someone please tell me how to increase this?
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}
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
:
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
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.
Beware this would not work if you need several instances of __MODULE__
.
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.