Helpers for working with millisecond timeout values.
Timeouts in OTP are a number of milliseconds, or one of the atoms :infinity
and :hibernate. This module computes a timeout from a target DateTime and
renders a duration as readable text, passing those atoms through untouched so
the helpers fit anywhere a timeout is expected.
iex> Ark.Timeout.until(:infinity)
:infinity
iex> IO.iodata_to_binary(Ark.Timeout.format(1_500))
"1s500ms"
Summary
Functions
Formats a millisecond duration as a compact human-readable string.
Formats a millisecond duration as a human-readable string in the chosen format.
Returns the number of milliseconds from now until date_time, clamped to [0, 4_294_967_295].
Returns the number of milliseconds from now until datetime, clamped to
[0, 4_294_967_295].
Functions
@spec format(non_neg_integer() | :infinity | :hibernate) :: iodata()
Formats a millisecond duration as a compact human-readable string.
Uses the :short format, for example "1d2h3m". Call format/2 to choose
the format. :infinity and :hibernate both render as "infinity". The
result is iodata/0.
iex> IO.iodata_to_binary(Ark.Timeout.format(1_500))
"1s500ms"
@spec format(non_neg_integer() | :infinity | :hibernate, :short | :long) :: iodata()
Formats a millisecond duration as a human-readable string in the chosen format.
The format argument selects the rendering:
:short- compact form, for example"1d2h3m":long- verbose form, for example"1 day 2 hours 3 minutes"
:infinity and :hibernate both render as "infinity". A negative duration
is prefixed with "-" in the short format and "(negative) " in the long
format. The result is iodata/0.
iex> IO.iodata_to_binary(Ark.Timeout.format(90_000, :long))
"1 minute 30 seconds"
@spec until(DateTime.t() | :infinity | :hibernate) :: non_neg_integer() | :infinity | :hibernate
Returns the number of milliseconds from now until date_time, clamped to [0, 4_294_967_295].
Also accepts :infinity or :hibernate, returning the atom unchanged, so the
result can be given to a receive or GenServer timeout.
iex> Ark.Timeout.until(:infinity)
:infinityTo measure against a fixed reference time, use until/2.
@spec until(DateTime.t() | :infinity | :hibernate, DateTime.t()) :: non_neg_integer() | :infinity | :hibernate
Returns the number of milliseconds from now until datetime, clamped to
[0, 4_294_967_295].
Like until/1, but takes the reference time explicitly instead of reading
DateTime.utc_now/0, which makes it deterministic to test. :infinity and
:hibernate are returned unchanged.
iex> now = ~U[2024-01-01 00:00:00Z]
iex> later = ~U[2024-01-01 00:00:10Z]
iex> Ark.Timeout.until(later, now)
10000