SetuClient.TokenManager (Setu Client v1.0.0)

Copy Markdown View Source

OTP GenServer managing Setu Bearer token lifecycle.

  • ETS table for lock-free reads across all processes
  • Proactive refresh 60 s before expiry (TTL 300 s)
  • Singleflight deduplication of concurrent refresh calls
  • :telemetry events on every refresh

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears the cached token for cfg, forcing a re-fetch on the next call.

Returns a valid Bearer token for cfg.

Types

cache_key()

@type cache_key() :: {String.t(), atom()}

fetch_result()

@type fetch_result() ::
  {:ok, String.t(), non_neg_integer()} | {:error, SetuClient.Error.t()}

state()

@type state() :: %{
  in_flight: %{optional(cache_key()) => {Task.t(), [GenServer.from()]}}
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

invalidate(cfg)

@spec invalidate(SetuClient.Config.t()) :: :ok

Clears the cached token for cfg, forcing a re-fetch on the next call.

token(cfg)

@spec token(SetuClient.Config.t()) ::
  {:ok, String.t()} | {:error, SetuClient.Error.t()}

Returns a valid Bearer token for cfg.

Reads from ETS (lock-free). Falls back to a GenServer call only on cache miss.