Railsr.Auth.TokenCache (Railsr v1.0.0)

Copy Markdown View Source

ETS-backed GenServer that caches the Railsr OAuth 2.0 bearer token and refreshes it automatically 60 seconds before expiry.

Behaviour

  • On first call to get_token/0, fetches a token from Railsr and stores it.
  • Schedules a refresh (expires_in - 60) seconds after fetch.
  • If the refresh fails, retries with full-jitter exponential back-off.
  • Thread-safe: concurrent callers during a refresh receive the existing valid token rather than all firing network requests.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the current valid bearer token string.

Forcibly evict the cached token (useful in tests or after 401s).

Start the token cache process

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_token()

@spec get_token() :: {:ok, String.t()} | {:error, Railsr.Error.t()}

Returns the current valid bearer token string.

Fetches synchronously on first call. On subsequent calls reads from ETS (lock-free) — safe to call from any process at high frequency.

invalidate()

@spec invalidate() :: :ok

Forcibly evict the cached token (useful in tests or after 401s).

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the token cache process