How to avoid GenServer from timing out?

defmodule Games.GuessingGame do
  use GenServer

  @moduledoc """
  Guess the number game

  def start() do
    GenServer.start_link(__MODULE__, [], name: :guessing_game)

  def current_score(pid) do, :current_score)

  def play(pid) do
    guess = prompt(), {:guess, guess})

  @spec play_helper(String.t(), integer(), integer()) :: integer()
  defp play_helper(answer, guess, attempts) do
    cond do
      guess == answer ->
        IO.puts(IO.ANSI.green_background() <> "Correct! You win!!!" <> IO.ANSI.reset())

      attempts == 0 ->
        IO.puts(IO.ANSI.red_background() <> "You lose! the answer was #{answer}" <> IO.ANSI.reset())

      attempts > 0 and guess < answer ->
        IO.puts(IO.ANSI.cyan_background() <> "Too Low!" <> IO.ANSI.reset())
        play_helper(answer, prompt(), attempts - 1)

      attempts > 0 and guess > answer ->
        IO.puts(IO.ANSI.magenta_background() <> "Too High!" <> IO.ANSI.reset())
        play_helper(answer, prompt(), attempts - 1)

  defp prompt() do
    IO.gets(IO.ANSI.reset() <> "Guess a number between 1 and 10: ")
    |> String.trim()
    |> String.to_integer()

  def init(_init_args) do
    {:ok, 0}

  def handle_call(:current_score, _from, score) do
    {:reply, score, score}

  def handle_call({:guess, guess}, _from, score) do
    updated_score = score + play_helper(Enum.random(1..10), guess, 5)
    {:reply, updated_score, updated_score}

I am trying to write the above guessing game. When playing first time, the prompt waits w/o timing out because the call hasn’t been made to GenServer yet. In the next attempt though it times out if response is not provided within 5 seconds.

I want the subsequent calls also to block until the user has provided an answer. I am not sure how to refactor this or use some option with GenServer for it to block.

you can pass :infinity as the third argument to

Yes, that is one option but I am not sure if that is a good way to go about it. I was hoping if there’s another workaround.

It would be nice if the game was exported to it’s own module, and have the gen_server having a game state, and a text client.

It is well explained in this non-free course

where the game is also a guessing game (hangman)

You are having a lot of responsabilities in your gen_server.

What if You want to have a web version?