Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearer documentation for GenServer timeouts #8776

Merged
merged 10 commits into from Feb 11, 2019

Conversation

@nathanl
Copy link
Contributor

nathanl commented Feb 6, 2019

I thought it was unclear what would happen if a timeout of 0 were set
but messages were already waiting.

@nathanl

This comment has been minimized.

Copy link
Contributor Author

nathanl commented Feb 6, 2019

Here's a little GenServer process I used to test how this works.

defmodule Timeout do
  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, 0, name: __MODULE__)
  end

  def init(count) do
    {:ok, count, 1_000}
  end

  def yo(server \\ __MODULE__) do
    GenServer.cast(server, :yo)
  end

  def handle_cast(:yo, count) do
    IO.puts("processing a message - count is #{count}")
    # simulate work
    :timer.sleep(1_000)

    case count do
      0 ->
        IO.puts("setting timeout of :infinity")
        {:noreply, count + 1, :infinity}

      1 ->
        IO.puts("setting timeout of 2 seconds")
        {:noreply, count + 1, 2_000}

      _n ->
        IO.puts("setting a timeout of 0")
        {:noreply, count + 1, 0}
    end
  end

  def handle_info(:timeout, count) do
    IO.puts("timeout!")
    {:noreply, 0}
  end
end

With that in a supervision tree, in iex -S mix, call Timeout.yo 0 times, once, or repeatedly, then wait to see when the handle_info is called.

lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
@nathanl nathanl force-pushed the nathanl:clearer-timeout-doc branch from 17fe9cc to 41c516c Feb 6, 2019
@nathanl

This comment has been minimized.

Copy link
Contributor Author

nathanl commented Feb 6, 2019

@eksperimental Thanks for the feedback. I also added a paragraph to discourage using a timeout when handle_continue is better.

I thought it was unclear what would happen if a timeout of `0` were set
but messages were already waiting.
@nathanl nathanl force-pushed the nathanl:clearer-timeout-doc branch from 41c516c to 8a98daf Feb 6, 2019
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
whatyouhide and others added 6 commits Feb 7, 2019
Co-Authored-By: nathanl <him@nathanmlong.com>
Co-Authored-By: nathanl <him@nathanmlong.com>
Co-Authored-By: nathanl <him@nathanmlong.com>
Co-Authored-By: nathanl <him@nathanmlong.com>
Co-Authored-By: nathanl <him@nathanmlong.com>
@nathanl

This comment has been minimized.

Copy link
Contributor Author

nathanl commented Feb 7, 2019

I pushed another commit to clarify what a timeout is actually useful for:

The timeout can be used to detect a lull in incoming messages.

... and to clarify the reason that a 0 timeout isn't reliable:

Because a message may arrive before the timeout is set, even a timeout of 0
milliseconds is not guaranteed to execute.

lib/elixir/lib/gen_server.ex Outdated Show resolved Hide resolved
fertapric and others added 2 commits Feb 11, 2019
Co-Authored-By: nathanl <him@nathanmlong.com>
@nathanl

This comment has been minimized.

Copy link
Contributor Author

nathanl commented Feb 11, 2019

@fertapric 👍 Updated

@josevalim josevalim merged commit b1c5e25 into elixir-lang:master Feb 11, 2019
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@josevalim

This comment has been minimized.

Copy link
Member

josevalim commented Feb 11, 2019

❤️ 💚 💙 💛 💜

@nathanl nathanl deleted the nathanl:clearer-timeout-doc branch Feb 12, 2019
Hanspagh added a commit to Hanspagh/elixir that referenced this pull request Mar 19, 2019
I thought it was unclear what would happen if a timeout of `0`
were set but messages were already waiting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.