Anton Prokofiev
Anton Prokofiev

Reputation: 957

erlang:send_after message delay

I have following code:

send_event_at({TsMsec,Msg}) -> 
    Now = os:system_time(micro_seconds),
    NowMsec = erlang:convert_time_unit(Now,micro_seconds,milli_seconds),
    DelayMsec = TsMsec - NowMsec,
    if DelayMsec >= 0  ->
            erlang:send_after(DelayMsec,self(),Msg);
      true -> ignore
end.

Then in gen_fsm I handle this messages as:

handle_info({new_status,{Status,HrQtKey}},StateName,State) ->
     .....
    {next_state,StateName,State};

Code used to send messages with delays up to 48 hours. Most of the time everything is Ok.

But if my gen_fsm has a decent amount of incoming messages, new_status messages are delaying up to 15 minutes.

This bug is appeared not too often but it is really annoying.

And idea what could be the reasons and what would be the best way to fix it?

Upvotes: 0

Views: 392

Answers (1)

Pascal
Pascal

Reputation: 14042

Some clues:

  • The time you receive is an absolute time and there are many time sources in erlang (see Time and Time Correction in Erlang), so there is a risk to combine the os:system_time with the Erlang Monotonic Time, and you should be aware of the time reference for the TsMsec parameter.
  • In my opinion it should be easier to use relative delay directly in your parameter.
  • The send_after function uses the Erlang Monotonic Time - from the documentation it is not clear if it then works with an absolute or relative time (though I think it is an absolute time). It is said that the Erlang Monitonic Time accuracy depends on
    • Accuracy and precision of OS monotonic time
    • Accuracy and precision of OS system time
    • time warp mode used

Upvotes: 2

Related Questions