Search⌘ K
AI Features

Set Timeouts

Understand how to implement timeouts in GenServer processes to handle inactivity by automatically shutting down unused processes. Explore setting timeout values, modifying reply tuples, and handling the :timeout message to stop processes as part of a supervised system, ensuring reliable resource management.

We'll cover the following...

There’s another case we need to handle before we’re done with this section. What happens if a player starts a game and then abandons it somewhere along the way? We don’t want to let those processes just sit there taking up system resources.

Fortunately, GenServer gives us an automated way to have processes time out and shut themselves down if they haven’t received a new message in a given number of milliseconds. We just need to do is add a fourth element to any of the GenServer reply tuples. This will be a positive integer representing the number of milliseconds to wait before timing out {:reply, :some_reply, %{}, 1000}.

Setting a test timeout

Let’s add some modifications to the Game module so game ...