feat: Token caching for external identity providers#935
feat: Token caching for external identity providers#935MagicAbdel wants to merge 3 commits intopgdogdev:mainfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Nice! Quick question: do you think it would be possible to run the token acquisition as a background task instead? That way, the token is always fresh when accessed for creating server connections. |
|
Sorry for the delay, I finally had some time to circle back to your comments. That was a great suggestion. I’ve added Let me know if this looks good! |
|
|
||
| let (token, expires_at) = fetcher(addr.clone()).await?; | ||
| set(key, token.clone(), expires_at); | ||
| spawn_refresh_task(addr.clone(), expires_at, fetcher); |
There was a problem hiding this comment.
There is a race condition here where multiple connections can spawn multiple spawn_refresh_tasks. You want to spawn one task at startup per configured address. A good place for this would be the connection pool (src/backend/pool/monitor.rs).
| return; | ||
| } | ||
|
|
||
| tokio::spawn(async move { |
There was a problem hiding this comment.
This loop needs to check for when this address becomes invalid, e.g. when the config is reloaded. Checkout the connection pool implementation, that might be a better place implementation for long-lived loops like this one.
Summary
Introduces an in-memory token cache shared by
azure_workload_identityandrds_iamauthentication backends. Tokens are now fetched once and reused until expiry, instead of being fetched on every connection.Motivation
Token fetching from external identity providers can be slow — Azure Workload Identity in particular was measured at ~30s per token fetch. This was directly impacting pool startup time, as each connection attempt would block waiting for a fresh token.
Changes
token_cachemodule with get/set helpers keyed by host, port, and userazure_workload_identityto extractfetch_token()returning(String, SystemTime), using theexpires_onfield from the Azure SDK response as the cache TTLrds_iamto follow the same pattern, with a fixed 15-minute TTL (RDS IAM tokens are valid for 15 minutes but the AWS SDK does not return an expiry)Impact