Is there a way to self terminate a genserver after no activity?

Ok, I think I finally get it now.

In init I can return {:ok, state, timeout}, and if this genserver does not receive any messages it will NOT just exit, but send itself a :timeout message. This must be handled in the the handle_info(:timeout, _state) in which I can then return a {:stop, :normal, state} which WILL cause the genserver to exit.

If the genserver receives a new message the original timeout is canceled. In order to renew it, the message handler (handle_info | handle_cast | handle_call) must return the timeout in a similar fashion that init originally did.

1 Like