Custom timer module

ludovic@demblans.com ludovic@REDACTED
Mon Jan 13 12:56:21 CET 2020


Hello,

I'm looking for advice about implementing a timer queue in erlang or elixir.

Basically what I need is a queue where I can write a timestamp, a key, and a value. When the local time reaches the timestamp, I want to handle the key/value (I guess receiving a message with the key and fetch, or receiving the key/value).
The timer must be cancelable as the value is very small but will lead to heavy computation. I want to be able to fetch a key from outside of the process at any time and run the computation (and then cancel the scheduled computation).

This is a subset of the `timer` module. But I need to persist the data to disk and reload on application start.

I will have a tiny amount of entries : around 1000.

At the moment I have an ordered_set with `{Timestamp, UserKey} = Key` as keys, I lookup for the first key and send_after to self the `Key` with `erlang:start_timer(max(TimeStamp, 1), self(), {run_deletion, Key})`, which returns a ref that I keep in state so I can ignore timer messages with an older ref. Everything is done inside a gen_server where I load the table from disk after init. Also I have to write the table to disk after each update because it is very important that all events are handled.

I'm about to implement cancellation but the code becomes messy as I do both table management and data management in the same module.

I wonder if there are implementations of this pattern in the community ? I could use a priority queue, but it seems to me that the implementations use a fixed list of priorities, and not arbitrary priorities (like a timestamp).

Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200113/d5517d59/attachment.htm>


More information about the erlang-questions mailing list