Could someone explain the best practice for handling token expiration in an async FastAPI proxy using httpx? I am building a middleware layer for Genesys Cloud APIs. Currently, I am using a Redis cache to store the access token with a TTL slightly less than the token’s expires_in.
Here is my current flow:
- Check Redis for a valid token.
- If missing or expired, acquire a distributed lock.
- Fetch new token via
/oauth/tokenusingrequests(sync within async context viarun_in_threadpool). - Update Redis and release lock.
The issue: Under high concurrency, multiple requests hit the lock simultaneously. While the first request fetches the token, others wait. When they acquire the lock, they see the token is still ‘fresh’ (just updated) and skip fetching. However, if the first request takes too long or fails, subsequent requests might timeout or get a stale token.
Is there a more robust pattern for async token management with httpx? I want to avoid the overhead of run_in_threadpool and handle 401 Unauthorized responses gracefully with exponential backoff. Any code examples appreciated.