Xirsys.XTurn.Timing (xturn v2.0.0)

Copy Markdown View Source

Allocation lifetime and refresh deadline helpers.

What problem this solves

TURN allocations, permissions, and channels expire unless refreshed. GenServer :timeout values must not jump when the system clock changes, so expiry uses monotonic deadlines. Legacy reporting still uses Gregorian seconds from :calendar where older state fields require it.

RFCs

Summary

Functions

Computes a monotonic deadline in milliseconds from a lifetime in seconds.

Returns the current local {date, time} tuple from :calendar.

Returns remaining milliseconds until expiry, or zero when already expired.

Returns remaining milliseconds from allocation start time and lifetime (Gregorian seconds).

Returns the current local time as Gregorian seconds since year zero.

Functions

deadline_ms(lifetime_seconds)

Computes a monotonic deadline in milliseconds from a lifetime in seconds.

Parameters

  • lifetime_seconds - allocation or permission lifetime in whole seconds

Examples

iex> deadline = Xirsys.XTurn.Timing.deadline_ms(10)
iex> is_integer(deadline)
true
iex> deadline > System.monotonic_time(:millisecond)
true

local_time()

Returns the current local {date, time} tuple from :calendar.

milliseconds_left(deadline_ms)

Returns remaining milliseconds until expiry, or zero when already expired.

Accepts a monotonic deadline, a map with :deadline_ms, a map with :refresh_time and :lifetime, or a (start_time, lifetime) pair.

milliseconds_left(start_time, lifetime)

Returns remaining milliseconds from allocation start time and lifetime (Gregorian seconds).

now()

Returns the current local time as Gregorian seconds since year zero.