Hi. I recently started picking up Elixir, and have been reading code here and there. I came across the Clock module on the Bandit repo.
This module updates a value in an ets table every second, and it is implemented using the Task module. My question is, what is the difference between this implementation and implementing the same functionality using GenServer? (more or less like this, I guess):
defmodule Bandit.ClockGen do
use GenServer
def date_header do
# exactly the same as in Bandit.Clock
end
def start_link(_opts) do
GenServer.start_link(__MODULE__, [])
end
@impl true
def init(_) do
__MODULE__ = :ets.new(__MODULE__, [:set, :protected, :named_table, {:read_concurrency, true}])
update_header()
{:ok, %{}}
end
@impl true
def handle_info(:update_header, state) do
update_header()
{:noreply, state}
end
def handle_info(_, state), do: {:noreply, state}
defp update_header do
:ets.insert(__MODULE__, {:date_header, get_date_header()})
Process.send_after(self(), :update_header, 1_000)
end
defp get_date_header, do: Calendar.strftime(DateTime.utc_now(), "%a, %d %b %Y %X GMT")
end
Are these two implementations equivalent? What am I missing?
Thank you.