ex_timer

Better-performance timer in elixir

not using extra gen_server and not using any other processes for timer

Overview

In general, It used a timer to schedule any works in the future.
Process.send_after/4 provides its function in elixir.
But, If you register a lot of timers, its mailbox getting larger.
In elixir as the number of queues increases, cause performance issues.
Maybe the most important point to note that you should be keep a small queue called as mailbox.

Specification

Installation

If available in Hex, add in deps of mix.exs

def deps do
  [
    {:ex_timer, "~> 0.1.0"}
  ]
end

then run as

$ mix deps.get

Usage

defmodule Scheduler do

  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, [])
  end

  def init(state) do
    Process.send_after(self(), :tick, 1000)
    ExTimer.add(state, {:timeout1, 1, 9}, 2000)
    ExTimer.add(state, {:timeout2, 3, 9}, 5000)
    {:ok, state}
  end

  def handle_info(:tick, state) do
    state = ExTimer.update(state)
    {:noreply, state}
  end
  
  def handle_info({:timeout1, arg0, arg1}, state) do
    IO.puts("#{inspect(__ENV__.function)} (#{arg0}, #{arg1}) called")
    {:noreply, state}
  end
  
  def handle_info({:timeout2, arg0, arg1}, state) do
    IO.puts("#{inspect(__ENV__.function)} (#{arg0}, #{arg1}) called")
    {:noreply, state}
  end
end