:timer.send_after not worked as expected

Hi,
I would like my channel grap some info and broadcast every 10 seconds.
Here is what I did in the handle_info method.

def handle_info("refresh", socket) do
    IO.puts "#{DateTime.utc_now()} get refresh message"
    cars = HL7MessageQue.ready_messages()
    if length(cars) > 0, do: broadcast!(socket, "cars", %{cars: cars})
    :timer.send_after(10000, "refresh")
    {:noreply, socket}
  end

But I find a strange thing that, I will receive more refresh message than I expected.
Any thining I missed here?

2019-10-24 02:11:03.296186Z get refresh message
2019-10-24 02:11:05.476439Z get refresh message
2019-10-24 02:11:06.445240Z get refresh message
2019-10-24 02:11:06.469283Z get refresh message
-----After 10 second----
2019-10-24 02:11:13.297116Z get refresh message (This is expected)
2019-10-24 02:11:15.478200Z get refresh message (But how these message come from?)
2019-10-24 02:11:16.446367Z get refresh message (But how these message come from?)
2019-10-24 02:11:16.470129Z get refresh message (But how these message come from?)
-----After 10 second----
2019-10-24 02:11:23.298133Z get refresh message
2019-10-24 02:11:25.480222Z get refresh message
2019-10-24 02:11:26.448112Z get refresh message
2019-10-24 02:11:26.472292Z get refresh message
2019-10-24 02:11:33.299192Z get refresh message
2019-10-24 02:11:35.481103Z get refresh message
2019-10-24 02:11:36.449251Z get refresh message
2019-10-24 02:11:36.474123Z get refresh message
2019-10-24 02:11:43.301203Z get refresh message

This suggests that you have an initial batch of four separate refresh messages.

Each of those four messages is repeated every 10_000 ms.

1 Like