CCXT.WS.Auth.Expiry (ccxt_client v0.6.1)

Copy Markdown View Source

Pure helpers for computing auth session expiry timing.

Used by the adapter layer (T94/T95) to decide when to schedule an :auth_expired re-auth timer. TTL sources in priority order:

  1. Response-level — auth response returns {:ok, %{ttl_ms: N}} from handle_auth_response/2 (e.g. deribit emits expires_in in seconds)
  2. Config-levelauth_config[:auth_ttl_ms] as a static override
  3. None — no TTL source, no timer scheduled (caller should not re-auth on a clock; rely on server-driven auth-failed errors instead)

Not a CCXT.WS.Auth.Behaviour implementer — this is a pure utility shared across patterns that return TTL metadata.

Example

auth_meta = %{ttl_ms: 900_000}
auth_config = %{pattern: :jsonrpc_linebreak}

ttl_ms = Expiry.compute_ttl_ms(auth_meta, auth_config)
# => 900_000

Expiry.schedule_delay_ms(ttl_ms)
# => 720_000  (80% safety margin)

Summary

Functions

Resolves the effective TTL (milliseconds) from response metadata and config.

Computes the delay before scheduling :auth_expired, applying the 80% safety margin and the 24h hard cap.

Functions

compute_ttl_ms(auth_meta, auth_config)

@spec compute_ttl_ms(map() | nil, map() | nil) :: pos_integer() | nil

Resolves the effective TTL (milliseconds) from response metadata and config.

Response-level TTL (auth_meta.ttl_ms) wins over config-level (auth_config[:auth_ttl_ms]). Returns nil when neither is available.

schedule_delay_ms(ttl_ms)

@spec schedule_delay_ms(pos_integer() | nil) :: pos_integer() | nil

Computes the delay before scheduling :auth_expired, applying the 80% safety margin and the 24h hard cap.

Returns nil for nil or non-positive inputs.