time_queue v0.8.1 TimeQueue

Implements a timers queue based on a list of maps. The queue can be encoded as JSON.

The performance will be worse for large queues in regard to the previous gb_trees based implementation, although the difference is negligible for small queues (<= 1000 entries).

All map keys are shrinked to a single letter as this queue is intended to be encoded and published to HTTP clients over the wire, mutiple times.

The queue keys are a map composed of the timestamp (t) of an entry and an unique integer (u).

No erlang timers or processes are used, as the queue is only a data structure. The advantage is that the queue can be persisted on storage and keeps working after restarting the runtime. The queue maintain its own list of unique integers to avoir relying on BEAM unique integers as they are reset on VM restarts.

The main drawback of a functional queue is that the queue entries must be manually checked for expired timers.

Link to this section Summary

Functions

Deletes an entry from the queue and returns the new queue.

Deletes all entries from the queue whose values are equal to unwanted.

Adds a new entry to the queue with a TTL and the current system time as now/0.

Adds a new entry to the queue with a TTL relative to the given timestamp in milliseconds.

Adds a new entry to the queue with an absolute timestamp.

Returns a new queue with entries for whom the given callback returned a truthy value.

Returns a new queue with entries for whom the given callback returned a truthy value.

Creates an empty time queue.

This function is used internally to determine the current time when using functions enqueue/3, pop/1, pop_entry/1 and peek_entry/1.

Returns the next value of the queue or a delay in milliseconds before the next value.

Returns the next value of the queue, or a delay, according to the given current time in milliseconds.

Returns the next entry of the queue or a delay in milliseconds before the next value.

Returns the next event of the queue according to the given current time in milliseconds.

Extracts the next entry in the queue or returns a delay.

Extracts the next entry in the queue according to the given current time in milliseconds.

Extracts the next entry in the queue with the current system time as now/0.

Extracts the next entry in the queue according to the given current time in milliseconds.

Returns the numer of entries in the queue.

Returns the time reference of an queue entry. This reference is used as a key to identify a unique entry.

Returns the value of an queue entry.

Link to this section Types

Link to this type

enqueue_return()

enqueue_return() :: {:ok, tref(), t()}
Link to this opaque

entry()

(opaque)
entry()
Link to this type

entry_value()

entry_value() :: any()
Link to this type

peek_entry_return()

peek_entry_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry()}
Link to this type

peek_return()

peek_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry_value()}
Link to this type

pop_entry_return()

pop_entry_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry(), t()}
Link to this type

pop_return()

pop_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry_value(), t()}
Link to this opaque

t()

(opaque)
t()
Link to this type

timespec()

timespec() :: {pos_integer(), timespec_unit()}
Link to this type

timespec_unit()

timespec_unit() ::
  :ms
  | :second
  | :seconds
  | :minute
  | :minutes
  | :hour
  | :hours
  | :day
  | :days
  | :week
  | :weeks
Link to this type

timestamp_ms()

timestamp_ms() :: pos_integer()
Link to this opaque

tref()

(opaque)
tref()
Link to this type

ttl()

ttl() :: timespec() | integer()

Link to this section Functions

Link to this function

delete(tq, tref)

delete(t(), entry() | tref()) :: t()

Deletes an entry from the queue and returns the new queue.

It accepts a time reference or a full entry. When an entry is given, its time reference will be used to find the entry to delete, meaning the queue entry will be deleted even if the value of the passed entry was tampered.

The function does not fail if the entry cannot be found and simply returns the queue as-is.

Link to this function

delete_val(tq, unwanted)

delete_val(t(), any()) :: t()

Deletes all entries from the queue whose values are equal to unwanted.

Link to this function

enqueue(tq, ttl, val)

enqueue(t(), ttl(), any()) :: enqueue_return()

Adds a new entry to the queue with a TTL and the current system time as now/0.

See enqueue/4.

Link to this function

enqueue(tq, ttl, val, now_ms)

enqueue(t(), ttl(), any(), now :: integer()) :: enqueue_return()

Adds a new entry to the queue with a TTL relative to the given timestamp in milliseconds.

Returns {:ok, tref, new_queue} where tref is a timer reference.

Link to this function

enqueue_abs(tq, ts, val)

enqueue_abs(t(), end_time :: integer(), value :: any()) :: enqueue_return()

Adds a new entry to the queue with an absolute timestamp.

Returns {:ok, tref, new_queue} where tref is a timer reference.

Link to this function

filter(tq, fun)

filter(t(), (entry() -> bool())) :: t()

Returns a new queue with entries for whom the given callback returned a truthy value.

Use filter_val/2 to filter only using values.

Link to this function

filter_val(tq, fun)

filter_val(t(), (any() -> bool())) :: t()

Returns a new queue with entries for whom the given callback returned a truthy value.

Unlinke filter/2, the callback is only passed the entry value.

Link to this function

new()

new() :: t()

Creates an empty time queue.

iex> tq = TimeQueue.new()
iex> TimeQueue.peek_entry(tq)
:empty

This function is used internally to determine the current time when using functions enqueue/3, pop/1, pop_entry/1 and peek_entry/1.

It is a simple alias to :erlang.system_time(:millisecond). TimeQueue does not use monotonic time since it already manages its own unique identifiers for queue entries.

Link to this function

peek(tq)

peek(t()) :: peek_return()

Returns the next value of the queue or a delay in milliseconds before the next value.

See peek/2.

Link to this function

peek(tq, now)

peek(t(), now_ms :: timestamp_ms()) :: peek_return()

Returns the next value of the queue, or a delay, according to the given current time in milliseconds.

Just like pop/2 vs.pop_entry/2, peek wil only return {:ok, value} when a timeout is reached whereas peek_entry will return {:ok, entry}.

Link to this function

peek_entry(tq)

peek_entry(t()) :: peek_entry_return()

Returns the next entry of the queue or a delay in milliseconds before the next value.

Entry values can be retrieved with TimeQueue.value/1.

See peek_entry/2.

Link to this function

peek_entry(map, now)

peek_entry(t(), now_ms :: timestamp_ms()) :: peek_entry_return()

Returns the next event of the queue according to the given current time in milliseconds.

Possible return values are:

  • :empty
  • {:ok, entry} if the timestamp of the first entry is <= to the given current time.
  • {:delay, tref, ms} if the timestamp of the first entry is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.new() |> TimeQueue.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.peek_entry(tq, _now = 20)
iex> {:ok, _} = TimeQueue.peek_entry(tq, _now = 100)

Extracts the next entry in the queue or returns a delay.

See pop/2.

Link to this function

pop(tq, now)

pop(t(), now_ms :: timestamp_ms()) :: pop_return()

Extracts the next entry in the queue according to the given current time in milliseconds.

Much like pop_entry/2 but the tuple returned when an entry time is reached (returns with :ok) success will only contain the value inserted in the queue.

Possible return values are:

  • :empty
  • {:ok, value, new_queue} if the timestamp of the first entry is <= to the given current time. The entry is deleted from new_queue.
  • {:delay, tref, ms} if the timestamp of the first entry is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.new() |> TimeQueue.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.pop(tq, _now = 20)
iex> {:ok, value, _} = TimeQueue.pop(tq, _now = 100)
iex> value
:hello
Link to this function

pop_entry(tq)

pop_entry(t()) :: pop_entry_return()

Extracts the next entry in the queue with the current system time as now/0.

See pop_entry/2.

Link to this function

pop_entry(tq, now)

pop_entry(t(), now_ms :: timestamp_ms()) :: pop_entry_return()

Extracts the next entry in the queue according to the given current time in milliseconds.

Possible return values are:

  • :empty
  • {:ok, entry, new_queue} if the timestamp of the first entry is <= to the given current time. The entry is deleted from new_queue.
  • {:delay, tref, ms} if the timestamp of the first entry is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.new() |> TimeQueue.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.pop_entry(tq, _now = 20)
iex> {:ok, entry, _} = TimeQueue.pop_entry(tq, _now = 100)
iex> TimeQueue.value(entry)
:hello
Link to this function

size(map)

size(t()) :: integer()

Returns the numer of entries in the queue.

Link to this function

supports_encoding(arg1)

Link to this function

tref(map)

tref(entry()) :: any()

Returns the time reference of an queue entry. This reference is used as a key to identify a unique entry.

iex> tq = TimeQueue.new()
iex> {:ok, tref, tq} = TimeQueue.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, entry} = TimeQueue.peek_entry(tq)
iex> tref == TimeQueue.tref(entry)
true
Link to this function

value(map)

value(entry()) :: any()

Returns the value of an queue entry.

iex> tq = TimeQueue.new()
iex> {:ok, _, tq} = TimeQueue.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, entry} = TimeQueue.peek_entry(tq)
iex> TimeQueue.value(entry)
:my_value