A simple mechanism to timeout idle Elixir processes - for example a GenServer - after a given period of inactivity.
- Good interface
- Minimal overhead
- Obvious implementation
Setup the timeout watchdog in GenServer.init/3:
IdleTimeout.start __MODULE__, 2000
First argument must be any unique ID, second argument is the first / default timeout. Your GenServer now has to ping the watchdog to prevent timeout:
IdleTimeout.ping __MODULE__
This will renew the last / default timeout. Optionally a new timeout can be given:
IdleTimeout.ping __MODULE__, 1000
Timeout only ever will get expanded, not shortened by later, shorter intervals.
Doesn't work too well with short timeouts. Should be fine with anyting in the seconds range. My own usecase is several minutes.
If available in Hex, the package can be installed
by adding idle_timeout_ex
to your list of dependencies in mix.exs
:
def deps do
[{:idle_timeout_ex, "~> 0.1.0"}]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/idle_timeout_ex.