refresh-oauth-tokens-without-a-race
Use when a client holds OAuth access tokens. Refresh before expiry behind a lock, persist rotated refresh tokens atomically, and handle the 401-then-retry path exactly once.
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 80
- Forks
- 17
01 前言 Front Matter
- name
- refresh-oauth-tokens-without-a-race
- description
- Use when a client holds OAuth access tokens. Refresh before expiry behind a lock, persist rotated refresh tokens atomically, and handle the 401-then-retry path exactly once.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md361 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Refresh OAuth tokens without a race
A token refresh implemented naively misfires three ways: it fires once per concurrent request, it loses a rotated refresh token, or it loops forever on a dead grant.
Procedure
- Store expiry as absolute UTC
expires_at, not a countdown, and refresh whennow >= expires_at - 60s. - Single-flight the refresh so N concurrent 401s trigger one token request, not N:
import threading, time _lock, _access, _expiry = threading.Lock(), None, 0.0 def token(): with _lock: if time.time() < _expiry - 60: return _access return _refresh() # sets _access/_expiry - When the provider rotates refresh tokens (returns a new
refresh_token), persist the new one before dropping the old — the old dies immediately on rotation. - On a business-call 401: refresh once and retry that call once. A second 401 means the grant is dead — surface it, do not loop.
- Store tokens at
chmod 600 ~/.config/app/token.jsonor in a secret store, never in logs or the repo. - Treat
invalid_grant(re-auth required, human) distinctly from 5xx (retry with backoff).
Pitfalls
- Two processes each refreshing a rotating token invalidate each other; coordinate via the store's advisory lock.
- Comparing local naive time to a server
expires_inacross timezones shifts expiry; do all maths in UTC. - Retrying every request N times on 401 turns an auth outage into a self-inflicted burst.
expires_inis usually seconds but some providers send milliseconds — check the unit.- Logging the token on a refresh failure leaks credentials into your log aggregator.
Verification
curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer $ACCESS" "$API/v1/me"
200 means the token path works; a persistent 401 means the refresh logic is broken. Report the token's expires_at and that a 401 cycle refreshed exactly once.
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.