IdleTimeout v0.1.2 IdleTimeout View Source
A simple mechanism to timeout idle Elixir processes - for example a GenServer - after a given period of inactivity.
Design goals
- Good interface
- Minimal overhead
- Obvious implementation
Interface
- Start your timeout with
IdleTimeout.start/1,2
. - On activity renew the timeout with
IdleTimeout.ping/1,2
. - Receive an
:EXIT
message when the timeout expires without activity.
Overhead
A single linked process is spawned.
Implementation.
The linked process receive
s :ping
messages. On timeout the process kills itself. Because it’s linked it will kill the parent process as well.
Link to this section Summary
Functions
This will renew the last / default timeout. Optionally a new timeout can be given
Setup the timeout watchdog - for example in GenServer.init/3
Link to this section Functions
This will renew the last / default timeout. Optionally a new timeout can be given.
Examples
iex> IdleTimeout.ping :some_id
{:ping, nil}
Setup the timeout watchdog - for example in GenServer.init/3:
IdleTimeout.start __MODULE__, 2000
First argument must be any unique ID (suitable for Process.register/2), second argument is the first / default timeout. Your GenServer now has to ping the watchdog to prevent timeout.
Examples
iex> IdleTimeout.start :some_id
#=> pid