Clever OTP tricks

What things in OTP-land using Processes, GenServers, Supervision Trees and the likes have you come across and found clever enough to tell other people about?


I myself recently learned about Process.send_after/3, and that it is entirely possible for a process to itself call send_after to remind itself about something later. I really like how one can combine it with GenServer.handle_info/2 to basically create a Process that ‘lets itself tick’.

It might have somewhat limited uses, but for my use case (entities governed by finite state machines that might change state depending on passed time) it works really gracefully.

5 Likes

Notice for state machines using the :gen_fsm behaviour there are some functions dealing with sending delayed events and messages to the process itself, such as start_timer/2 which will send a term to the current state in the form {:timeout, _timer, *term*}; and send_event_after/2 which will send a change event message to the state machine. Both returns a reference that can be used to cancel the timer using cancel_timer/1.

5 Likes